42 import java.awt.Color;
43 import java.awt.Dimension;
44 import java.awt.Graphics;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
57 @SuppressWarnings(
"serial")
85 Dimension initialSize =
null;
91 initialSize = getSize();
92 int a[] =
new int[initialSize.height / 2];
93 double f = initialSize.width / (double)
a.length;
95 for (
int i =
a.length; --i >= 0;) {
98 for (
int i =
a.length; --i >= 0;) {
99 int j = (int) (i * Math.random());
130 if (kicker !=
null) {
135 }
catch (InterruptedException e) {
144 String at = getParameter(
"alg");
149 algName = at +
"Algorithm";
153 addMouseListener(
this);
161 showStatus(getParameter(
"alg"));
169 removeMouseListener(
this);
180 int deltaY = 0, deltaX = 0, evenY = 0;
182 Dimension currentSize = getSize();
183 int currentHeight = currentSize.height;
184 int currentWidth = currentSize.width;
194 if (!currentSize.equals(initialSize)) {
195 evenY = (currentHeight - initialSize.height) % 2;
196 deltaY = (currentHeight - initialSize.height) / 2;
197 deltaX = (currentWidth - initialSize.width) / 2;
209 g.setColor(getBackground());
210 y = currentHeight - deltaY - 1;
211 for (
int i =
a.length; --i >= 0; y -= 2) {
212 g.drawLine(deltaX + arr[i], y, currentWidth, y);
216 g.setColor(Color.black);
217 y = currentHeight - deltaY - 1;
218 for (
int i =
a.length; --i >= 0; y -= 2) {
219 g.drawLine(deltaX, y, deltaX + arr[i], y);
223 g.setColor(Color.red);
224 y = deltaY + evenY + h1 * 2 + 1;
225 g.drawLine(deltaX, y, deltaX + initialSize.width, y);
228 g.setColor(Color.blue);
229 y = deltaY + evenY + h2 * 2 + 1;
230 g.drawLine(deltaX, y, deltaX + initialSize.width, y);
252 if (algorithm ==
null) {
253 algorithm = (
SortAlgorithm) Class.forName(algName).newInstance();
258 }
catch (Exception e) {
267 public synchronized void stop() {
268 if (algorithm !=
null) {
271 }
catch (IllegalThreadStateException e) {
286 if (kicker ==
null || !kicker.isAlive()) {
287 kicker =
new Thread(
this);
294 showStatus(getParameter(
"alg"));
320 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.";
327 "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." }