FileDocCategorySizeDatePackage
SortItem.javaAPI DocSun JDK 1.4.2 Example7298Thu May 12 00:35:30 BST 2005None

SortItem

public class SortItem extends Applet implements MouseListener, Runnable
A simple applet class to demonstrate a sort algorithm. You can specify a sorting algorithm using the "alg" attribyte. When you click on the applet, a thread is forked which animates the sorting algorithm.
author
James Gosling
version
1.17f, 10 Apr 1995

Fields Summary
private Thread
kicker
The thread that is sorting (or null).
int[]
arr
The array that is being sorted.
int
h1
The high water mark.
int
h2
The low water mark.
String
algName
The name of the algorithm.
SortAlgorithm
algorithm
The sorting algorithm (or null).
Constructors Summary
Methods Summary
public voiddestroy()
Deallocate resources of applet.

        removeMouseListener(this);
    
public java.lang.StringgetAppletInfo()

    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.";
  
public java.lang.String[][]getParameterInfo()

    String[][] info = {
      {"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."}
    };
    return info;
  
public voidinit()
Initialize the applet.

	String at = getParameter("alg");
	if (at == null) {
	    at = "BubbleSort";
	}

	algName = at + "Algorithm";
	scramble();

	resize(100, 100);
	addMouseListener(this);
    
public voidmouseClicked(java.awt.event.MouseEvent e)

public voidmouseEntered(java.awt.event.MouseEvent e)

public voidmouseExited(java.awt.event.MouseEvent e)

public voidmousePressed(java.awt.event.MouseEvent e)

public voidmouseReleased(java.awt.event.MouseEvent e)
The user clicked in the applet. Start the clock!

    startSort();
    e.consume();
  
public voidpaint(java.awt.Graphics g)
Paint the array of numbers as a list of horizontal lines of varying lengths.

	int a[] = arr;
	int y = getSize().height - 1;

	// Erase old lines
	g.setColor(getBackground());
	for (int i = a.length; --i >= 0; y -= 2) {
	    g.drawLine(arr[i], y, getSize().width, y);
	}

	// Draw new lines
	g.setColor(Color.black);
	y = getSize().height - 1;
	for (int i = a.length; --i >= 0; y -= 2) {
	    g.drawLine(0, y, arr[i], y);
	}

	if (h1 >= 0) {
	    g.setColor(Color.red);
	    y = h1 * 2 + 1;
	    g.drawLine(0, y, getSize().width, y);
	}
	if (h2 >= 0) {
	    g.setColor(Color.blue);
	    y = h2 * 2 + 1;
	    g.drawLine(0, y, getSize().width, y);
	}
    
voidpause()
Pause a while.

see
SortAlgorithm

	pause(-1, -1);
    
voidpause(int H1)
Pause a while, and draw the high water mark.

see
SortAlgorithm

	pause(H1, -1);
    
voidpause(int H1, int H2)
Pause a while, and draw the low&high water marks.

see
SortAlgorithm

	h1 = H1;
	h2 = H2;
	if (kicker != null) {
	    repaint();
	}
	try {Thread.sleep(20);} catch (InterruptedException e){}
    
public voidrun()
Run the sorting algorithm. This method is called by class Thread once the sorting algorithm is started.

see
java.lang.Thread#run
see
SortItem#mouseUp

	try {
	    if (algorithm == null) {
		algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
		algorithm.setParent(this);
	    }
	    algorithm.init();
	    algorithm.sort(arr);
	} catch(Exception e) {
	}
    
voidscramble()
Fill the array with random numbers from 0..n-1.


                 
      
	int a[] = new int[getSize().height / 2];
	double f = getSize().width / (double) a.length;
	for (int i = a.length; --i >= 0;) {
	    a[i] = (int)(i * f);
	}
	for (int i = a.length; --i >= 0;) {
	    int j = (int)(i * Math.random());
	    int t = a[i];
	    a[i] = a[j];
	    a[j] = t;
	}
	arr = a;
    
public voidstart()

        scramble();
        repaint();
    
private synchronized voidstartSort()
For a Thread to actually do the sorting. This routine makes sure we do not simultaneously start several sorts if the user repeatedly clicks on the sort item. It needs to be synchronoized with the stop() method because they both manipulate the common kicker variable.

	if (kicker == null || !kicker.isAlive()) {
	    kicker = new Thread(this);
	    kicker.start();
	}
    
public synchronized voidstop()
Stop the applet. Kill any sorting algorithm that is still sorting.

	if (algorithm != null){
            try {
		algorithm.stop();
            } catch (IllegalThreadStateException e) {
                // ignore this exception
            }
            kicker = null;
	}
    
public voidupdate(java.awt.Graphics g)
Update without erasing the background.

	paint(g);