blob: c13bea9857e805e1073ce0becf55a763211c1368 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 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 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Sun Microsystems nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 */
34
35import java.awt.*;
36import java.awt.event.*;
37import java.io.InputStream;
38import java.util.Hashtable;
39import java.net.*;
40
41/**
42 * A simple applet class to demonstrate a sort algorithm.
43 * You can specify a sorting algorithm using the "alg"
44 * attribyte. When you click on the applet, a thread is
45 * forked which animates the sorting algorithm.
46 *
47 * @author James Gosling
48 */
49public class SortItem
50 extends java.applet.Applet
51 implements Runnable, MouseListener {
52
53 /**
54 * The thread that is sorting (or null).
55 */
56 private Thread kicker;
57
58 /**
59 * The array that is being sorted.
60 */
61 int arr[];
62
63 /**
64 * The high water mark.
65 */
66 int h1 = -1;
67
68 /**
69 * The low water mark.
70 */
71 int h2 = -1;
72
73 /**
74 * The name of the algorithm.
75 */
76 String algName;
77
78 /**
79 * The sorting algorithm (or null).
80 */
81 SortAlgorithm algorithm;
82
83 Dimension initialSize = null;
84
85 /**
86 * Fill the array with random numbers from 0..n-1.
87 */
88 void scramble() {
89 initialSize = getSize();
90 int a[] = new int[initialSize.height / 2];
91 double f = initialSize.width / (double) a.length;
92
93 for (int i = a.length; --i >= 0;) {
94 a[i] = (int)(i * f);
95 }
96 for (int i = a.length; --i >= 0;) {
97 int j = (int)(i * Math.random());
98 int t = a[i];
99 a[i] = a[j];
100 a[j] = t;
101 }
102 arr = a;
103 }
104
105 /**
106 * Pause a while.
107 * @see SortAlgorithm
108 */
109 void pause() {
110 pause(-1, -1);
111 }
112
113 /**
114 * Pause a while, and draw the high water mark.
115 * @see SortAlgorithm
116 */
117 void pause(int H1) {
118 pause(H1, -1);
119 }
120
121 /**
122 * Pause a while, and draw the low&high water marks.
123 * @see SortAlgorithm
124 */
125 void pause(int H1, int H2) {
126 h1 = H1;
127 h2 = H2;
128 if (kicker != null) {
129 repaint();
130 }
131 try {Thread.sleep(20);} catch (InterruptedException e){}
132 }
133
134 /**
135 * Initialize the applet.
136 */
137 public void init() {
138 String at = getParameter("alg");
139 if (at == null) {
140 at = "BubbleSort";
141 }
142
143 algName = at + "Algorithm";
144 scramble();
145
146 resize(100, 100);
147 addMouseListener(this);
148 }
149
150 public void start() {
151 h1 = h2 = -1;
152 scramble();
153 repaint();
154 showStatus(getParameter("alg"));
155 }
156
157 /**
158 * Deallocate resources of applet.
159 */
160 public void destroy() {
161 removeMouseListener(this);
162 }
163
164 /**
165 * Paint the array of numbers as a list
166 * of horizontal lines of varying lengths.
167 */
168 public void paint(Graphics g) {
169 int a[] = arr;
170 int y = 0;
171 int deltaY = 0, deltaX = 0, evenY = 0, evenX = 0;
172
173 Dimension currentSize = getSize();
174 int currentHeight = currentSize.height;
175 int currentWidth = currentSize.width;
176
177 // Check to see if the applet has been resized since it
178 // started running. If so, need the deltas to make sure
179 // the applet is centered in its containing panel.
180 // The evenX and evenY are because the high and low
181 // watermarks are calculated from the top, but the rest
182 // of the lines are calculated from the bottom, which
183 // can lead to a discrepancy if the window is not an
184 // even size.
185 if (!currentSize.equals(initialSize)) {
186 evenY = (currentHeight - initialSize.height) % 2;
187 evenX = (currentWidth - initialSize.width) % 2;
188 deltaY = (currentHeight - initialSize.height) / 2;
189 deltaX = (currentWidth - initialSize.width) / 2;
190
191 if (deltaY < 0) {
192 deltaY = 0;
193 evenY = 0;
194 }
195 if (deltaX < 0) {
196 deltaX = 0;
197 evenX = 0;
198 }
199 }
200
201 // Erase old lines
202 g.setColor(getBackground());
203 y = currentHeight - deltaY - 1;
204 for (int i = a.length; --i >= 0; y -= 2) {
205 g.drawLine(deltaX + arr[i], y, currentWidth, y);
206 }
207
208 // Draw new lines
209 g.setColor(Color.black);
210 y = currentHeight - deltaY - 1;
211 for (int i = a.length; --i >= 0; y -= 2) {
212 g.drawLine(deltaX, y, deltaX + arr[i], y);
213 }
214
215 if (h1 >= 0) {
216 g.setColor(Color.red);
217 y = deltaY + evenY + h1 * 2 + 1;
218 g.drawLine(deltaX, y, deltaX + initialSize.width, y);
219 }
220 if (h2 >= 0) {
221 g.setColor(Color.blue);
222 y = deltaY + evenY + h2 * 2 + 1;
223 g.drawLine(deltaX, y, deltaX + initialSize.width, y);
224 }
225 }
226
227 /**
228 * Update without erasing the background.
229 */
230 public void update(Graphics g) {
231 paint(g);
232 }
233
234 /**
235 * Run the sorting algorithm. This method is
236 * called by class Thread once the sorting algorithm
237 * is started.
238 * @see java.lang.Thread#run
239 * @see SortItem#mouseUp
240 */
241 public void run() {
242 try {
243 if (algorithm == null) {
244 algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
245 algorithm.setParent(this);
246 }
247 algorithm.init();
248 algorithm.sort(arr);
249 } catch(Exception e) {
250 }
251 }
252
253 /**
254 * Stop the applet. Kill any sorting algorithm that
255 * is still sorting.
256 */
257 public synchronized void stop() {
258 if (algorithm != null){
259 try {
260 algorithm.stop();
261 } catch (IllegalThreadStateException e) {
262 // ignore this exception
263 }
264 kicker = null;
265 }
266 }
267
268 /**
269 * For a Thread to actually do the sorting. This routine makes
270 * sure we do not simultaneously start several sorts if the user
271 * repeatedly clicks on the sort item. It needs to be
272 * synchronized with the stop() method because they both
273 * manipulate the common kicker variable.
274 */
275 private synchronized void startSort() {
276 if (kicker == null || !kicker.isAlive()) {
277 kicker = new Thread(this);
278 kicker.start();
279 }
280 }
281
282
283 public void mouseClicked(MouseEvent e) {
284 showStatus(getParameter("alg"));
285 }
286
287 public void mousePressed(MouseEvent e) {
288 }
289
290 /**
291 * The user clicked in the applet. Start the clock!
292 */
293 public void mouseReleased(MouseEvent e) {
294 startSort();
295 e.consume();
296 }
297
298 public void mouseEntered(MouseEvent e) {
299 }
300
301 public void mouseExited(MouseEvent e) {
302 }
303
304 public String getAppletInfo() {
305 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.";
306 }
307
308 public String[][] getParameterInfo() {
309 String[][] info = {
310 {"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."}
311 };
312 return info;
313 }
314}