home *** CD-ROM | disk | FTP | other *** search
/ Computer Life: Multimedia Mega Pac / Multimedia_Mega-Pac_Computer_Life_1996.iso / hotjava / demo / classes / sortitem.jav < prev    next >
Text File  |  1995-05-19  |  5KB  |  236 lines

  1. /*
  2.  * @(#)SortItem.java    1.17 95/04/10 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import awt.*;
  21. import browser.NameServer;
  22. import java.io.InputStream;
  23. import java.util.Hashtable;
  24. import net.www.html.*;
  25. import browser.Applet;
  26.  
  27. /**
  28.  * A simple applet class to demonstrate a sort algorithm.
  29.  * You can specify a sorting algorithm using the "alg"
  30.  * attribyte. When you click on the applet, a thread is
  31.  * forked which animates the sorting algorithm.
  32.  *
  33.  * @author James Gosling
  34.  * @version     1.17, 10 Apr 1995
  35.  */
  36. public
  37. class SortItem extends Applet implements Runnable {
  38.     /**
  39.      * The thread that is sorting (or null).
  40.      */
  41.     private Thread kicker;
  42.  
  43.     /**
  44.      * The array that is being sorted.
  45.      */
  46.     int arr[];
  47.  
  48.     /**
  49.      * The high water mark.
  50.      */
  51.     int h1 = -1;
  52.  
  53.     /**
  54.      * The low water mark.
  55.      */
  56.     int h2 = -1;
  57.  
  58.     /**
  59.      * The name of the algorithm.
  60.      */
  61.     String algName;
  62.  
  63.     /**
  64.      * The sorting algorithm (or null).
  65.      */
  66.     SortAlgorithm algorithm;
  67.  
  68.     /**
  69.      * Fill the array with random numbers from 0..n-1.
  70.      */
  71.     void scramble() {
  72.     int a[] = new int[height / 2];
  73.     double f = width / (double) a.length;
  74.     for (int i = a.length; --i >= 0;) {
  75.         a[i] = (int)(i * f);
  76.     }
  77.     for (int i = a.length; --i >= 0;) {
  78.         int j = (int)(i * Math.random());
  79.         int t = a[i];
  80.         a[i] = a[j];
  81.         a[j] = t;
  82.     }
  83.     arr = a;
  84.     }
  85.  
  86.     /**
  87.      * Pause a while.
  88.      * @see SortAlgorithm
  89.      */
  90.     void pause() {
  91.     pause(-1, -1);
  92.     }
  93.  
  94.     /**
  95.      * Pause a while, and draw the high water mark.
  96.      * @see SortAlgorithm
  97.      */
  98.     void pause(int H1) {
  99.     pause(H1, -1);
  100.     }
  101.  
  102.     /**
  103.      * Pause a while, and draw the low&high water marks.
  104.      * @see SortAlgorithm
  105.      */
  106.     void pause(int H1, int H2) {
  107.     h1 = H1;
  108.     h2 = H2;
  109.     if (kicker != null) {
  110.         repaint();
  111.     }
  112.     Thread.sleep(20);
  113.     }
  114.  
  115.     /**
  116.      * Initialize the applet.
  117.      */
  118.     public void init() {
  119.     resize(100, 100);
  120.  
  121.     String at = getAttribute("alg");
  122.     if (at == null) {
  123.         at = "BubbleSort";
  124.     }
  125.  
  126.     algName = at + "Algorithm";
  127.     scramble();
  128.     }
  129.  
  130.     /**
  131.      * Paint the array of numbers as a list
  132.      * of horizontal lines of varying lenghts.
  133.      */
  134.     public void paint(Graphics g) {
  135.     int a[] = arr;
  136.     int y = height - 1;
  137.  
  138.     // Erase old lines
  139.     g.setForeground(Color.lightGray);
  140.     for (int i = a.length; --i >= 0; y -= 2) {
  141.         g.drawLine(arr[i], y, width, y);
  142.     }
  143.  
  144.     // Draw new lines
  145.     g.setForeground(Color.black);
  146.     y = height - 1;
  147.     for (int i = a.length; --i >= 0; y -= 2) {
  148.         g.drawLine(0, y, arr[i], y);
  149.     }
  150.  
  151.     if (h1 >= 0) {
  152.         g.setForeground(Color.red);
  153.         y = h1 * 2 + 1;
  154.         g.drawLine(0, y, width, y);
  155.     }
  156.     if (h2 >= 0) {
  157.         g.setForeground(Color.blue);
  158.         y = h2 * 2 + 1;
  159.         g.drawLine(0, y, width, y);
  160.     }
  161.     }
  162.  
  163.     /**
  164.      * Update without erasing the background.
  165.      */
  166.     public void update(Graphics g) {
  167.     paint(g);
  168.     }
  169.  
  170.     /**
  171.      * Run the sorting algorithm. This method is
  172.      * called by class Thread once the sorting algorithm
  173.      * is started.
  174.      * @see java.lang.Thread#run
  175.      * @see SortItem#mouseUp
  176.      */
  177.     public void run() {
  178.     try {
  179.         if (algorithm == null) {
  180.         algorithm = (SortAlgorithm)new(algName);
  181.         algorithm.setParent(this);
  182.         }
  183.         algorithm.init();
  184.         algorithm.sort(arr);
  185.     } catch(Object e) {
  186.     }
  187.     }
  188.  
  189.     /**
  190.      * Stop the applet. Kill any sorting algorithm that
  191.      * is still sorting.
  192.      */
  193.     public synchronized void stop() {
  194.     if (kicker != null) {
  195.             try {
  196.         kicker.stop();
  197.             } catch (IllegalStateException e) {
  198.                 // ignore this exception
  199.             }
  200.         kicker = null;
  201.     }
  202.     if (algorithm != null){
  203.             try {
  204.         algorithm.stop();
  205.             } catch (IllegalStateException e) {
  206.                 // ignore this exception
  207.             }
  208.     }
  209.     }
  210.  
  211.  
  212.     /**
  213.      * For a Thread to actually do the sorting. This routine makes
  214.      * sure we do not simultaneously start several sorts if the user
  215.      * repeatedly clicks on the sort item.  It needs to be
  216.      * synchronoized with the stop() method because they both
  217.      * manipulate the common kicker variable.
  218.      */
  219.     private synchronized void startSort() {
  220.     if (kicker == null || !kicker.isAlive()) {
  221.         scramble();
  222.         repaint();
  223.         kicker = new Thread(this);
  224.         kicker.start();
  225.     }
  226.     }
  227.  
  228.  
  229.     /**
  230.      * The user clicked in the applet. Start the clock!
  231.      */
  232.     public void mouseUp(int x, int y) {
  233.     startSort();
  234.     }
  235. }
  236.