Methods Summary |
---|
public void | destroy()Deallocate resources of applet.
removeMouseListener(this);
|
public java.lang.String | getAppletInfo()
return "Title: SortDemo \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' attribute. \nWhen you click on the applet, a thread is forked which animates \nthe sorting algorithm.";
|
public java.lang.String[][] | getParameterInfo()
String[][] info = {
{"alg", "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 void | init()Initialize the applet.
String at = getParameter("alg");
if (at == null) {
at = "BubbleSort";
}
algName = at + "Algorithm";
scramble();
resize(100, 100);
addMouseListener(this);
|
public void | mouseClicked(java.awt.event.MouseEvent e)
showStatus(getParameter("alg"));
|
public void | mouseEntered(java.awt.event.MouseEvent e)
|
public void | mouseExited(java.awt.event.MouseEvent e)
|
public void | mousePressed(java.awt.event.MouseEvent e)
|
public void | mouseReleased(java.awt.event.MouseEvent e)The user clicked in the applet. Start the clock!
startSort();
e.consume();
|
public void | paint(java.awt.Graphics g)Paint the array of numbers as a list
of horizontal lines of varying lengths.
int a[] = arr;
int y = 0;
int deltaY = 0, deltaX = 0, evenY = 0, evenX = 0;
Dimension currentSize = getSize();
int currentHeight = currentSize.height;
int currentWidth = currentSize.width;
// Check to see if the applet has been resized since it
// started running. If so, need the deltas to make sure
// the applet is centered in its containing panel.
// The evenX and evenY are because the high and low
// watermarks are calculated from the top, but the rest
// of the lines are calculated from the bottom, which
// can lead to a discrepancy if the window is not an
// even size.
if (!currentSize.equals(initialSize)) {
evenY = (currentHeight - initialSize.height) % 2;
evenX = (currentWidth - initialSize.width) % 2;
deltaY = (currentHeight - initialSize.height) / 2;
deltaX = (currentWidth - initialSize.width) / 2;
if (deltaY < 0) {
deltaY = 0;
evenY = 0;
}
if (deltaX < 0) {
deltaX = 0;
evenX = 0;
}
}
// Erase old lines
g.setColor(getBackground());
y = currentHeight - deltaY - 1;
for (int i = a.length; --i >= 0; y -= 2) {
g.drawLine(deltaX + arr[i], y, currentWidth, y);
}
// Draw new lines
g.setColor(Color.black);
y = currentHeight - deltaY - 1;
for (int i = a.length; --i >= 0; y -= 2) {
g.drawLine(deltaX, y, deltaX + arr[i], y);
}
if (h1 >= 0) {
g.setColor(Color.red);
y = deltaY + evenY + h1 * 2 + 1;
g.drawLine(deltaX, y, deltaX + initialSize.width, y);
}
if (h2 >= 0) {
g.setColor(Color.blue);
y = deltaY + evenY + h2 * 2 + 1;
g.drawLine(deltaX, y, deltaX + initialSize.width, y);
}
|
void | pause()Pause a while.
pause(-1, -1);
|
void | pause(int H1)Pause a while, and draw the high water mark.
pause(H1, -1);
|
void | pause(int H1, int H2)Pause a while, and draw the low&high water marks.
h1 = H1;
h2 = H2;
if (kicker != null) {
repaint();
}
try {Thread.sleep(20);} catch (InterruptedException e){}
|
public void | run()Run the sorting algorithm. This method is
called by class Thread once the sorting algorithm
is started.
try {
if (algorithm == null) {
algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
algorithm.setParent(this);
}
algorithm.init();
algorithm.sort(arr);
} catch(Exception e) {
}
|
void | scramble()Fill the array with random numbers from 0..n-1.
initialSize = getSize();
int a[] = new int[initialSize.height / 2];
double f = initialSize.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 void | start()
h1 = h2 = -1;
scramble();
repaint();
showStatus(getParameter("alg"));
|
private synchronized void | startSort()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
synchronized 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 void | stop()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 void | update(java.awt.Graphics g)Update without erasing the background.
paint(g);
|