/* AlgThread.java Generic algorithm running thread .. all algorithms implement specialisations of this .. overriding methods as they need */ package ciips.animation; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; /** Animations should extend this class and implement the run and loadData methods. */ public abstract class AlgThread extends Thread implements ItemListener, InputCompleteListener { static int max_data = 10; static String base_data_file_name = "graph.rb"; public AlgAnimFrame frame; // Parent frame public DrawingPanel[] d_panel; static final int offset_y = 10; static final int offset_x = 50; private int frames = 1; // Number of consecutive frames to be shown private static final boolean DEBUG = true; public AlgThread() { System.out.println("AlgThread cons complete"); } public void setParms( AlgAnimFrame frame, int frames ) { if(frame == null) { System.out.println("AlgThread: null frame"); } System.out.println("AlgThread - " + frames + " frames" ); this.frame = frame; this.frames = frames; d_panel = new DrawingPanel[frames]; if( DEBUG ) System.out.println("AlgThread:setParms complete"); } public void setParms( AlgAnimFrame frame, String ds[], int frames ) { setParms( frame, frames ); // dataSets = ds; } public void setParms( AlgAnimFrame frame, String df_prefix, int frames ) { this.setParms( frame, frames ); // Now locate all the data files // dataSets = getFileNames( df_prefix ); } // public String[] getDataSets() { return dataSets; } // Added in a setDelay function here public void setDelay(int delay) { frame.getCurrentPanel().setDelay(delay); } public int getDataPanelCount() { return frames; } /** Shuffle the previous panels down * Panel with index = 0 is the current panel **/ public void shuffleDown( ) { if ( DEBUG ) System.out.println("AlgThread:shuffleDown"); if ( frames == 1 ) return; // nothing to do for( int k=1;kchoice will be a data set index in the range 0..max_data_sets-1 */ public abstract boolean loadData( int choice ); /** Generate the example data set. Reads the chosen data set from the frame's menu, and calls loadData( choice ) to load or generate the appropriate data set. */ public void generateData() { //int choice = frame.control_panel.getDataChoice(); //added // this.dpAfter = frame.getDrawingPanel(); frame.getCurrentPanel().init(); int choice = frame.getDataChoice(); if ( loadData( choice ) ) { System.out.println("Data loaded OK"); } else { System.out.println("Data loading error"); } } /** Run the animation. This is the key method that the animator provides: it starts up the animation and runs it to completion. */ public abstract void run(); public void restoreDrawingPanel() { // This needs to be here because it gets called from AlgAnimFrame } //This method needs to be here for the skip button to be effective public void waitSkip() { DrawingPanel dpAfter = frame.getCurrentPanel(); if(!dpAfter.getSkip()) return; ((ImageButton)frame.getSkipItem()).setEnable(); ((ImageButton)frame.getRunItem()).setEnable(); ((ImageButton)frame.getStopItem()).setDisable(); dpAfter.setSkip(false); frame.setStep(true); frame.waitStep(); } /** Return a list of options that can be set when running the algorithm: override this method in the sub-class if there are any such options. This method returns null. **/ public String[] getOptions() { return null; } /** Options are added to the Options menu: when their state changes, the algorithm thread will be notified. Override this method to handle changes in the options. **/ public void itemStateChanged( ItemEvent e ) { } /** Options are added to the Options menu: when their state changes, the algorithm thread will be notified. Override this method to handle changes in the options. **/ public void optionChanged( int index, boolean new_state ) { if( DEBUG ) System.out.println("AlgThread: option " + index + " becomes " + new_state ); } /** Allow the animator to create a special menu of user actions associated with this animation. To create a special actions menu, override this method in the algorithm thread. If this method returns a non-null menu, it will be added to the animation frame **/ public Menu getActionMenu() { return null; } public void inputComplete( InputDialogEvent e ) {} }