home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / applets / SortDemo / SortItem.java < prev   
Encoding:
Java Source  |  2002-09-06  |  7.1 KB  |  285 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)SortItem.java    1.12 02/06/13
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.awt.event.*;
  42. import java.io.InputStream;
  43. import java.util.Hashtable;
  44. import java.net.*;
  45.  
  46. /**
  47.  * A simple applet class to demonstrate a sort algorithm.
  48.  * You can specify a sorting algorithm using the "alg"
  49.  * attribyte. When you click on the applet, a thread is
  50.  * forked which animates the sorting algorithm.
  51.  *
  52.  * @author James Gosling
  53.  * @version     1.17f, 10 Apr 1995
  54.  */
  55. public class SortItem
  56.     extends java.applet.Applet
  57.     implements Runnable, MouseListener {
  58.     /**
  59.      * The thread that is sorting (or null).
  60.      */
  61.     private Thread kicker;
  62.  
  63.     /**
  64.      * The array that is being sorted.
  65.      */
  66.     int arr[];
  67.  
  68.     /**
  69.      * The high water mark.
  70.      */
  71.     int h1 = -1;
  72.  
  73.     /**
  74.      * The low water mark.
  75.      */
  76.     int h2 = -1;
  77.  
  78.     /**
  79.      * The name of the algorithm.
  80.      */
  81.     String algName;
  82.  
  83.     /**
  84.      * The sorting algorithm (or null).
  85.      */
  86.     SortAlgorithm algorithm;
  87.  
  88.     /**
  89.      * Fill the array with random numbers from 0..n-1.
  90.      */
  91.     void scramble() {
  92.     int a[] = new int[getSize().height / 2];
  93.     double f = getSize().width / (double) a.length;
  94.     for (int i = a.length; --i >= 0;) {
  95.         a[i] = (int)(i * f);
  96.     }
  97.     for (int i = a.length; --i >= 0;) {
  98.         int j = (int)(i * Math.random());
  99.         int t = a[i];
  100.         a[i] = a[j];
  101.         a[j] = t;
  102.     }
  103.     arr = a;
  104.     }
  105.  
  106.     /**
  107.      * Pause a while.
  108.      * @see SortAlgorithm
  109.      */
  110.     void pause() {
  111.     pause(-1, -1);
  112.     }
  113.  
  114.     /**
  115.      * Pause a while, and draw the high water mark.
  116.      * @see SortAlgorithm
  117.      */
  118.     void pause(int H1) {
  119.     pause(H1, -1);
  120.     }
  121.  
  122.     /**
  123.      * Pause a while, and draw the low&high water marks.
  124.      * @see SortAlgorithm
  125.      */
  126.     void pause(int H1, int H2) {
  127.     h1 = H1;
  128.     h2 = H2;
  129.     if (kicker != null) {
  130.         repaint();
  131.     }
  132.     try {Thread.sleep(20);} catch (InterruptedException e){}
  133.     }
  134.  
  135.     /**
  136.      * Initialize the applet.
  137.      */
  138.     public void init() {
  139.     String at = getParameter("alg");
  140.     if (at == null) {
  141.         at = "BubbleSort";
  142.     }
  143.  
  144.     algName = at + "Algorithm";
  145.     scramble();
  146.  
  147.     resize(100, 100);
  148.     addMouseListener(this);
  149.     }
  150.  
  151.     public void start() {
  152.         scramble();
  153.         repaint();
  154.     }
  155.  
  156.     /**
  157.      * Deallocate resources of applet.
  158.      */
  159.     public void destroy() {
  160.         removeMouseListener(this);
  161.     }
  162.  
  163.     /**
  164.      * Paint the array of numbers as a list
  165.      * of horizontal lines of varying lengths.
  166.      */
  167.     public void paint(Graphics g) {
  168.     int a[] = arr;
  169.     int y = getSize().height - 1;
  170.  
  171.     // Erase old lines
  172.     g.setColor(getBackground());
  173.     for (int i = a.length; --i >= 0; y -= 2) {
  174.         g.drawLine(arr[i], y, getSize().width, y);
  175.     }
  176.  
  177.     // Draw new lines
  178.     g.setColor(Color.black);
  179.     y = getSize().height - 1;
  180.     for (int i = a.length; --i >= 0; y -= 2) {
  181.         g.drawLine(0, y, arr[i], y);
  182.     }
  183.  
  184.     if (h1 >= 0) {
  185.         g.setColor(Color.red);
  186.         y = h1 * 2 + 1;
  187.         g.drawLine(0, y, getSize().width, y);
  188.     }
  189.     if (h2 >= 0) {
  190.         g.setColor(Color.blue);
  191.         y = h2 * 2 + 1;
  192.         g.drawLine(0, y, getSize().width, y);
  193.     }
  194.     }
  195.  
  196.     /**
  197.      * Update without erasing the background.
  198.      */
  199.     public void update(Graphics g) {
  200.     paint(g);
  201.     }
  202.  
  203.     /**
  204.      * Run the sorting algorithm. This method is
  205.      * called by class Thread once the sorting algorithm
  206.      * is started.
  207.      * @see java.lang.Thread#run
  208.      * @see SortItem#mouseUp
  209.      */
  210.     public void run() {
  211.     try {
  212.         if (algorithm == null) {
  213.         algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
  214.         algorithm.setParent(this);
  215.         }
  216.         algorithm.init();
  217.         algorithm.sort(arr);
  218.     } catch(Exception e) {
  219.     }
  220.     }
  221.  
  222.     /**
  223.      * Stop the applet. Kill any sorting algorithm that
  224.      * is still sorting.
  225.      */
  226.     public synchronized void stop() {
  227.     if (algorithm != null){
  228.             try {
  229.         algorithm.stop();
  230.             } catch (IllegalThreadStateException e) {
  231.                 // ignore this exception
  232.             }
  233.             kicker = null;
  234.     }
  235.     }
  236.  
  237.     /**
  238.      * For a Thread to actually do the sorting. This routine makes
  239.      * sure we do not simultaneously start several sorts if the user
  240.      * repeatedly clicks on the sort item.  It needs to be
  241.      * synchronoized with the stop() method because they both
  242.      * manipulate the common kicker variable.
  243.      */
  244.     private synchronized void startSort() {
  245.     if (kicker == null || !kicker.isAlive()) {
  246.         kicker = new Thread(this);
  247.         kicker.start();
  248.     }
  249.     }
  250.  
  251.   //1.1 event handling
  252.  
  253.   public void mouseClicked(MouseEvent e)
  254.   {}
  255.  
  256.   public void mousePressed(MouseEvent e)
  257.   {}
  258.  
  259.   /**
  260.    * The user clicked in the applet. Start the clock!
  261.    */
  262.  
  263.   public void mouseReleased(MouseEvent e) {
  264.     startSort();
  265.     e.consume();
  266.   }
  267.  
  268.   public void mouseEntered(MouseEvent e)
  269.   {}
  270.  
  271.   public void mouseExited(MouseEvent e)
  272.   {}
  273.  
  274.   public String getAppletInfo() {
  275.     return "Title: ImageMap \nAuthor: James Gosling 1.17f, 10 Apr 1995 \nA simple applet class to demonstrate a sort algorithm.  \nYou can specify a sorting algorithm using the 'alg' attribyte.  \nWhen you click on the applet, a thread is forked which animates \nthe sorting algorithm.";
  276.   }
  277.  
  278.   public String[][] getParameterInfo() {
  279.     String[][] info = {
  280.       {"als", "string", "The name of the algorithm to run.  You can choose from the provided algorithms or suppply your own, as long as the classes are runnable as threads and their names end in 'Algorithm.'  BubbleSort is the default.  Example:  Use 'QSort' to run the QSortAlgorithm class."}
  281.     };
  282.     return info;
  283.   }
  284. }
  285.