⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortitem.java

📁 几个java的程序
💻 JAVA
字号:
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.该小应用程序揭示一种排序算法 * 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. */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.被排序的数组     */    int arr[];    /**     * The high water mark.高水位标记     */    int h1 = -1;    /**     * The low water mark.低水位标记     */    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的随机数填满数组     */    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.暂停,见SortAlgorithm程序      */    void pause() {	pause(-1, -1);    }    /**     * Pause a while, and draw the high water mark.暂停,并且标出高水位     * 见SortAlgorithm程序     */    void pause(int H1) {	pause(H1, -1);    }    /**     * Pause a while, and draw the low&high water marks.暂停,并且标出高水位&低水位     * @see 见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.初始化小应用程序     */    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将数组中的不同数字用长度不同的水平线段来表示出来     * 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.采用不消除背景的技术进行更新     */    public void update(Graphics g) {	paint(g);    }    /**     * 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     */    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.停止小应用程序,杀死一切正在进行的排序算法     */    public synchronized void stop() {	if (kicker != null) {            try {		kicker.stop();            } catch (IllegalThreadStateException e) {                // ignore this exception忽略这个异常            }	    kicker = null;	}	if (algorithm != null){            try {		algorithm.stop();            } catch (IllegalThreadStateException e) {                // ignore this exception忽略这个异常            }	}    }    /**     * 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.     */    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这里使用一个内部类来真正实现该小应用程序中所有的事件处理工作     * 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.为了使这个类能使用startSort方法,这里将私有属性去掉     */    class ClockStarter extends MouseAdapter {        /**         * The user clicked in the applet. Start the clock!当用户单击小应用程序时将启动计时器         */	public void mouseReleased(MouseEvent e) {	    startSort();	}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -