/* AlgThread.java */ import java.awt.*; import java.io.*; import java.applet.*; import java.net.*; import java.util.*; /** * AlgThread is an extension of the provided Java * multi-threading package. * It enables the graphical display of the main execution thread to be * updated during the execution of the algorithm. *

* A section of this source file is normally displayed on the TextFrame. * This section starts with the line after * /*------------------*/ * and finishes before the line //------------ * . * Every line starting with /*-*/ will be discarded in * the source code display area and each line is terminated by the first * encounter of /*-*/. *

* The source file AlgThread.java is to be * modified/completed for separate animation algorithms. * * @authorWoi L Ang * @sinceJDK1.0 */ public class AlgThread extends Thread { /** * Size of the data set appropriate for algorighm animation. */ public int max_data = 7; /** * Array of strings used to set the choices of the data choice button * in data menu. */ public static String[] dataSets = {"Data Set 1", "Data Set 2", "Data Set 3"}; private static String[] URLs = { "http://ciips.ee.uwa.edu.au/~koh-ta/BD/BD.form", "BD/BD.form", "http://ciips.ee.uwa.edu.au/~koh-ta/BD/BD1.form" }; /** * The initial caller of the constructor of this class. * It holds the references to all existing panels. */ public AlgAnimFrame frame; /** * A reference to the drawing panel in order to update the animation * during the execution of the algorithm. * @see DrawingPanel */ public DrawingPanel drawingPanel; BlockDiagram bd; //TransDiagram td; /** * Creates a new thread using the frame passed in as the parameter. * If this constructor is called from the frame constructor, * a drawingPanel will be initialized and assigned to the frame * class. * * @param frame An extended frame where the algorithm is going to * execute in. * * @see AlgAnimFrame */ public AlgThread(AlgAnimFrame frame) { this.frame = frame; this.drawingPanel = frame.getDrawingPanel(); if (frame != null && frame.getAlg() != null && frame.getAlg().drawingPanel != null) { // drawingPanel already created -> this constructor called from // clicking the run button -> use the generated data set } } /** * Sets the delay between each animation step. This determines * the rate the drawingPanel is updated. The setDelay * method is normally called by the action listener of * the delay choice button on the control panel. * @param delay The delay set in milliseconds. */ public void setDelay(int delay) { drawingPanel.setDelay(delay); } /** * Generate data based on the choice made on the menu. * This method is application specific * and the contents for the satisfaction of each 'if' statement * have to be filled in based on the algorithm. */ public void generateData() { URL url; this.drawingPanel = frame.getDrawingPanel(); int choice = frame.getDataChoice(); System.out.println("genData " + choice ); switch( choice ) { case 0: bd = new BlockDiagram("Test"); bd.ConsTestCase(); break; case 1: case 2: System.out.println("URL: " + URLs[choice] ); URL homeURL = frame.getApplet().getCodeBase(); try { url = new URL( homeURL, URLs[choice] ); bd = new BlockDiagram( url ); } catch( Exception e ) { System.out.println("Error forming URL from " + URLs[choice] ); } break; default: System.out.println("Invalid choice"); } System.out.println("generateData: exit" ); drawingPanel.init(); } /*--------------------------------------------------------------------*/ //---- /** * This method is invoked when the Thread class * start() method is called. * It contains the statements to execute the methods which perform * the algorithm. This method is to be completed based on separate * animation algorithms. * @see java.lang.Thread */ public void run() { generateData(); frame.getTextFrame(); // alg here drawingPanel.addDrawingObj( bd ); // finish sorting frame.finishAlg(); } /** * Restore the drawing panel at the beginning or the end of each * simulation. */ public void restoreDrawingPanel() { } /** * This method is to be placed after the line where the fast * forward function is to be performed. */ public void waitSkip() { if (!drawingPanel.getSkip()) return; ((ImageButton)frame.getSkipItem()).setEnable(); ((ImageButton)frame.getRunItem()).setEnable(); ((ImageButton)frame.getStopItem()).setDisable(); drawingPanel.setSkip(false); frame.setStep(true); frame.waitStep(); } }