/* AlgThread.java */ import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import ciips.animation.*; /** * 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. * * @author Woi L Ang * @since JDK1.0 */ public class MatMultAlgThread extends AlgThread implements ItemListener, ActionListener { /** * Size of the data set appropriate for algorighm animation. */ public int max_data = 6; /** Set the number of comment panel lines needed **/ static { AlgAnimFrame.COMPANEL_LINES = 1; } static { ciips.animation.UIFrame.allowUI( false ); } /** * Array of strings used to set the choices of the data choice button * in data menu. */ public static String[] MMDataSets = { "6 Matrices", "Small Data Set" }; private Dimension[] data; private OptMatMult optMatMult; /** * The initial caller of the constructor of this class. * It holds the references to all existing panels. */ private AlgAnimFrame f; static private int seq = 0; // Sequence number for this thread private int this_seq; /** * A reference to the drawing panel in order to update the animation * during the execution of the algorithm. * @see DrawingPanel */ // private BasicDrawingPanel DPanels[]; private DrawingPanel drawingPanel; // Ref to DPanels[0] static private final int N_HISTORY_PANELS = 1; static private final int MAT_ROW_HT = 19; /** * 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 MatMultAlgThread( 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 this.max_data = frame.getAlg().max_data; } } **/ public MatMultAlgThread( AlgAnimFrame frame ) { super(); setParms( frame, MMDataSets, N_HISTORY_PANELS ); // setParms( frame, N_HISTORY_PANELS ); f = frame; this_seq = seq++; System.out.println(this.getClass().getName() + " " + this_seq + " constructed OK"); } /** * 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 boolean loadData( int choice ) { System.out.println("MatMultAlgThread:loadData( " + choice + ")" ); // BasicDrawingPanel[] DPanels = frame.getDrawingPanel(); // drawingPanel = (DrawingPanel)(DPanels[0]); drawingPanel = f.getCurrentPanel(); drawingPanel.init(); if (choice == 0) { max_data = 6; data = new Dimension[max_data]; data[0] = new Dimension(100, 10); // col row data[1] = new Dimension(5, 100); data[2] = new Dimension(50, 5); data[3] = new Dimension(70, 50); data[4] = new Dimension(7, 70); data[5] = new Dimension(10, 7); initData( data ); } else if (choice == 1) { max_data = 3; data = new Dimension[max_data]; data[0] = new Dimension(100, 10); // col row data[1] = new Dimension(5, 100); data[2] = new Dimension(50, 5); initData(data); } return true; } private void initData( Dimension[] dim ) { // show matrix dimensions IntMatrix dimMat = new IntMatrix(dim.length, 2); dimMat.move( 20, 20 ); dimMat.setColor(Color.gray, Color.white); String[] colLabels = { "Rows ", "Columns" }; dimMat.setColLabels(colLabels); Subscript[] subs = new Subscript[dim.length]; for (int i = 0; i < dim.length; i++) { subs[i] = new Subscript(" A", ""+i); subs[i].setColor(Color.white); dimMat.setElem(0, i, dim[i].height); dimMat.setElem(1, i, dim[i].width); } dimMat.setRowLabels(subs); drawingPanel.addDrawingObj(dimMat); // label for best Matrix ShadowLabel dimLabel = new ShadowLabel("Dimension of Matrices"); dimLabel.move(30 + 3*66, 20 + MAT_ROW_HT*(dim.length/2)); drawingPanel.addDrawingObj(dimLabel); // show Cost matrix IntMatrix costMat = new IntMatrix(dim.length); costMat.blankAboveDiag(); costMat.setColor(Color.blue, Color.white); costMat.move( 20, 20 + (dim.length + 2)*MAT_ROW_HT ); costMat.setRowLabels(subs); costMat.setColLabels(subs); drawingPanel.addDrawingObj(costMat); // label for cost matrix ShadowLabel costLabel = new ShadowLabel("Cost Matrix"); costLabel.move(30 + (dim.length + 1)*66, 20 + (dim.length + 2 + dim.length/2)*MAT_ROW_HT); costLabel.setColor(Color.blue); drawingPanel.addDrawingObj(costLabel); // show Best matrix IntMatrix bestMat = new IntMatrix(dim.length); bestMat.blankAboveDiag(); bestMat.setColor(Color.red, Color.white); bestMat.move(20, 20 + 2*(dim.length + 2)*MAT_ROW_HT); bestMat.setRowLabels(subs); bestMat.setColLabels(subs); drawingPanel.addDrawingObj(bestMat); // label for best Matrix ShadowLabel bestLabel = new ShadowLabel("Best Matrix"); bestLabel.move(30 + (dim.length + 1)*66, 20 + (2*(dim.length + 2) + dim.length/2)*MAT_ROW_HT); bestLabel.setColor(Color.red); drawingPanel.addDrawingObj(bestLabel); // Representation note ShadowLabel noteLabel = new ShadowLabel("Each entry in the BEST MATRIX represents " + "the index of the last matrix in the left parenthesisation."); noteLabel.setColor(Color.red); noteLabel.move(20, 20+3*(dim.length + 2)*MAT_ROW_HT); drawingPanel.addDrawingObj(noteLabel); optMatMult = new OptMatMult(dim, costMat, bestMat, drawingPanel, frame, dimMat); drawingPanel.repaint(); } /*--------------------------------------------------------------------*/ //---- /** * 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(); // alg here optMatMult.start(); // finish sorting frame.finishAlg(); frame.setText(0, "Execution completed successfully ... Click 'run' to restart."); } /** * 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(); } public void actionPerformed( ActionEvent e ) { int index = -1; MenuItem source = (MenuItem)e.getSource(); /** for( int k=0;k= 0 ) { switch( index ) { case ADD_ACTION: if( DEBUG ) System.out.println("ADD action"); // Don't create a new one! if ( AddDialog == null ) AddDialog = new InputDialog( frame, "Enter value of item to add" ); AddDialog.show(); if( DEBUG ) System.out.println("RBTAlgThread:actPerf - wait for input" ); break; case DEL_ACTION: if( DEBUG ) System.out.println("DEL action"); // Don't create a new one! if ( DeleteDialog == null ) DeleteDialog = new InputDialog( frame, "Enter value of item to delete" ); DeleteDialog.show(); break; case PRINT_ACTION: System.out.println("PRINT action"); break; } } else { System.out.println("RBTreeAlgThread:actionPerformed - unknown action"); } **/ } }