home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / java / threads / example-1.1 / SortItem1_1.java < prev   
Encoding:
Java Source  |  1997-07-13  |  5.3 KB  |  236 lines

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