// // ControlPanel.java // Anneal // // Created by sw on Thu Sep 12 2002. // Copyright (c) 2001 __MyCompanyName__. All rights reserved. // package net.tiac.home.sw.anneal; import java.lang.Integer; import java.awt.*; import java.awt.event.*; import java.applet.Applet; class ControlPanel extends Panel { private Applet myApplet; private Board myBoard; private Label initLabel; private Choice initChoice; private InitPattern [] initValues; private InitPattern currentInitChoice; private Label speedLabel; private Choice speedChoice; private CheapVector speedValueV; private int speedValues []; private Button resetButton; private Button reverseButton; private Button pauseButton; private Button playButton; private TextField odometer; private int mileage; private int direction; // State variables for the pause button: private boolean run; private boolean pressedAfterPause; // Constructor: public ControlPanel( Applet myApplet ) { // Set up everything needed to lay out. this.myApplet = myApplet; initLabel = new Label( "Init Pattern:", Label.RIGHT ); // ===== THE INIT PATTERN CHOICE ===== this.initChoice = new Choice( ) { public boolean action( Event evt, Object what ) { if( currentInitChoice != initValues[ initChoice.getSelectedIndex() ] ) { sendInitPattern(); sendInit( true ); } return( true ); } }; initValues = new InitPatternPlethora( ).patterns; // The name strings affect the width given to the control: for( int i = 0; i < initValues.length; i++ ) { initChoice.addItem( initValues[i].name() ); // initValues[i] set above. } this.speedLabel = new Label(" Speed:", Label.RIGHT ); // ===== THE SPEED CHOICE ===== this.speedChoice = new Choice( ) { public boolean action( Event evt, Object what ) { sendSpeedDirection( ); return( true ); } }; // These strings affect the width given to the control: speedValueV = new CheapVector( new Integer [1], 0 ); speedChoice.addItem( "manual step" ); speedValueV.add( new Integer( 0 ) ); speedChoice.addItem( "1 step/sec" ); speedValueV.add( new Integer( 1 ) ); speedChoice.addItem( "10 step/sec" ); speedValueV.add( new Integer( 10 ) ); speedChoice.addItem( "100 step/sec" ); speedValueV.add( new Integer( 100 ) ); speedChoice.addItem( "1000 step/sec" ); speedValueV.add( new Integer( 1000 ) ); // speedChoice.addItem( "10000 step/sec" ); speedValueV.add( new Integer( 10000 ) ); speedValues = speedValueV.toIntArrayClear(); speedChoice.select( "1000 step/sec" ); // All the following button descriptions are // anonymous classes, a Java 1.1 thing. // ===== THE RESET BUTTON ===== this.resetButton = new Button( "|<<" ) { public boolean action( Event evt, Object what ) { setRun( false ); sendInit( true ); return( true ); } }; this.resetButton.setBackground( Color.white ); // ===== THE REVERSE BUTTON ===== this.reverseButton = new Button( "<" ) { public boolean action( Event evt, Object what ) { play( -1 ); return( true ); } }; this.reverseButton.setBackground( Color.white ); // ===== THE PAUSE BUTTON ===== // Pauses or un-pauses when you "click," // i.e. press and release without dragging. this.pauseButton = new Button( "||" ) { public boolean action( Event evt, Object what ) { setRun( !run ); return( true ); } }; this.pauseButton.setBackground( Color.white ); // ===== THE PLAY BUTTON ===== this.playButton = new Button( ">" ) { public boolean action( Event evt, Object what ) { play( +1 ); return( true ); } }; this.playButton.setBackground( Color.white ); this.odometer = new TextField( 10 ); this.odometer.setEditable( false ); this.odometer.setBackground( Color.white ); this.setLayout( new BorderLayout() ); Panel topRow = new Panel( ); topRow.setLayout( new FlowLayout( FlowLayout.LEFT, 3, 2 ) ); Panel buttons = new Panel( ); buttons.setLayout( new FlowLayout( FlowLayout.LEFT, 3, 2 ) ); Panel bottomRow = new Panel( ); bottomRow.setLayout( new BorderLayout( ) ); this.add( "North", topRow ); this.add( "South", bottomRow ); topRow.add( initLabel ); topRow.add( initChoice ); topRow.add( speedLabel ); topRow.add( speedChoice ); buttons.add( resetButton ); buttons.add( reverseButton ); buttons.add( pauseButton ); buttons.add( playButton ); bottomRow.add( "West", buttons ); bottomRow.add( "East", odometer ); } public synchronized void setRun( boolean run ) { this.run = run; this.pressedAfterPause = false; this.sendSpeedDirection(); } public int getSpeed( ) { return( speedValues[ speedChoice.getSelectedIndex() ] ); } public void sendSpeedDirection( ) { myBoard.setSpeedDirection( getSpeed(), this.run? direction : 0 ); } public synchronized void play( int direction ) { this.direction = direction; this.setRun( true ); if( getSpeed() == 0 ) { myBoard.step( ); this.setRun( false ); } } public void init( Board myBoard ) { this.myBoard = myBoard; this.setRun( false ); this.setMileage( 0 ); sendInitPattern( ); sendInit( true ); } public void sendInitPattern( ) { currentInitChoice = initValues[ initChoice.getSelectedIndex() ]; myBoard.setInitPattern( currentInitChoice ); } public void sendInit( boolean force ) { if( !force && ( myBoard.supposedToBeGoing( ) || this.getMileage() != 0 ) ) { // myApplet.showStatus( "Press \"|<<\" to start over." ); } else { // myApplet.showStatus( "" ); setRun( false ); myBoard.signalStopWaitForResponse( ); myBoard.init( 1, 1, this ); } } public void start( ) { // Applet calls this when it's started by browser. myBoard.start(); } public void stop( ) { // Applet calls this when it's stopped by browser. myBoard.stop(); } public int getMileage( ) { return( this.mileage ); } public void setMileage( int mileage ) { this.mileage = mileage; this.odometer.setText( " " + mileage ); } }