?? sortitem.java
字號(hào):
import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.InputStream;import java.util.Hashtable;import java.net.*;/** * A simple applet class to demonstrate a sort algorithm.該小應(yīng)用程序揭示一種排序算法 * You can specify a sorting algorithm using the "alg"讀者可以使用參數(shù)來(lái)確定使用的算法 * attribyte. When you click on the applet, a thread is當(dāng)用鼠標(biāo)單擊該小應(yīng)用程序時(shí),屏幕上將使用動(dòng)畫來(lái)顯示算法的執(zhí)行過(guò)程 * forked which animates the sorting algorithm. */public class SortItem extends java.applet.Applet implements Runnable { /** * The thread that is sorting (or null).排序線程 */ private Thread kicker; /** * The array that is being sorted.被排序的數(shù)組 */ int arr[]; /** * The high water mark.高水位標(biāo)記 */ int h1 = -1; /** * The low water mark.低水位標(biāo)記 */ int h2 = -1; /** * The name of the algorithm.排序算法的名稱 */ String algName; /** * The sorting algorithm (or null).排序算法 */ SortAlgorithm algorithm; /** * Fill the array with random numbers from 0...使用從0到n-1的隨機(jī)數(shù)填滿數(shù)組 */ void scramble() { 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; } /** * Pause a while.暫停,見(jiàn)SortAlgorithm程序 */ void pause() { pause(-1, -1); } /** * Pause a while, and draw the high water mark.暫停,并且標(biāo)出高水位 * 見(jiàn)SortAlgorithm程序 */ void pause(int H1) { pause(H1, -1); } /** * Pause a while, and draw the low&high water marks.暫停,并且標(biāo)出高水位&低水位 * @see 見(jiàn)SortAlgorithm程序 */ void pause(int H1, int H2) { h1 = H1; h2 = H2; if (kicker != null) { repaint(); } try {Thread.sleep(20);} catch (InterruptedException e){} } /** * Initialize the applet.初始化小應(yīng)用程序 */ public void init() { String at = getParameter("alg"); if (at == null) { at = "BubbleSort"; } algName = at + "Algorithm"; scramble(); addMouseListener(new ClockStarter()); resize(100, 100); } /** * Paint the array of numbers as a list將數(shù)組中的不同數(shù)字用長(zhǎng)度不同的水平線段來(lái)表示出來(lái) * of horizontal lines of varying lenghts. */ public void paint(Graphics g) { 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); } } /** * Update without erasing the background.采用不消除背景的技術(shù)進(jìn)行更新 */ public void update(Graphics g) { paint(g); } /** * Run the sorting algorithm. This method is * called by class Thread once the sorting algorithm * is started.執(zhí)行排序算法,排序算法一旦開(kāi)始就啟動(dòng)該方法 * @see java.lang.Thread#run * @see SortItem#mouseUp */ public void run() { try { if (algorithm == null) { algorithm = (SortAlgorithm)Class.forName(algName).newInstance(); algorithm.setParent(this); } algorithm.init(); algorithm.sort(arr); } catch(Exception e) { } } /** * Stop the applet. Kill any sorting algorithm that * is still sorting.停止小應(yīng)用程序,殺死一切正在進(jìn)行的排序算法 */ public synchronized void stop() { if (kicker != null) { try { kicker.stop(); } catch (IllegalThreadStateException e) { // ignore this exception忽略這個(gè)異常 } kicker = null; } if (algorithm != null){ try { algorithm.stop(); } catch (IllegalThreadStateException e) { // ignore this exception忽略這個(gè)異常 } } } /** * For a Thread to actually do the sorting. This routine makes為了使一個(gè)線程真正實(shí)現(xiàn)排序,確認(rèn)當(dāng)用戶重復(fù)的在小應(yīng)用程序上單擊鼠標(biāo)時(shí),不同時(shí)啟動(dòng)多個(gè)線程 * 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. */ synchronized void startSort() { if (kicker == null || !kicker.isAlive()) { scramble(); repaint(); kicker = new Thread(this); kicker.start(); } } /** * Here's an inner class that implements all the event handling這里使用一個(gè)內(nèi)部類來(lái)真正實(shí)現(xiàn)該小應(yīng)用程序中所有的事件處理工作 * for this applet. Wow, this simplifies the code! I had to * remove the "private" modifier from the startSort method so * that this class could use startSort.為了使這個(gè)類能使用startSort方法,這里將私有屬性去掉 */ class ClockStarter extends MouseAdapter { /** * The user clicked in the applet. Start the clock!當(dāng)用戶單擊小應(yīng)用程序時(shí)將啟動(dòng)計(jì)時(shí)器 */ public void mouseReleased(MouseEvent e) { startSort(); } }}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -