// Scott DeRuiter 5/2/02 // AnimateDerivative.java // Animates a series of images import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AnimateDerivative extends JApplet { private Image [] image; private int i, pausetime; ImagePanel canvas; private JButton runit, stopit, faster, slower, onestep; private JTextField describeField, numberField; public AnimateDerivative ( ) { image = new Image [12]; i = 0; pausetime = 450; } public void init() { setBackground ( Color.black ); canvas = new ImagePanel ( ); getContentPane().add( canvas, BorderLayout.CENTER ); JPanel left = new JPanel ( new GridLayout ( 5, 1 ) ); getContentPane().add ( BorderLayout.WEST, left ); runit = new JButton ( "RUN / RESUME" ); left.add ( runit ); runit.addActionListener ( canvas ); stopit = new JButton ( "STOP" ); left.add ( stopit ); stopit.addActionListener ( canvas ); onestep = new JButton ( "NEXT STEP" ); left.add ( onestep ); onestep.addActionListener ( canvas ); faster = new JButton ( "FASTER" ); left.add ( faster ); faster.addActionListener ( canvas ); slower = new JButton ( "SLOWER" ); left.add ( slower ); slower.addActionListener ( canvas ); JPanel bottom = new JPanel ( ); getContentPane().add ( BorderLayout.SOUTH, bottom ); describeField = new JTextField ( "Pause Time in Milliseconds:" ); describeField.setEditable ( false ); bottom.add ( describeField ); numberField = new JTextField ( 8 ); numberField.setText ( "500" ); bottom.add ( numberField ); for ( int i = 0; i < image.length; i++ ) { image[i] = getImage ( getDocumentBase ( ), (i + 1) + ".jpg" ); WaitForImage ( this, image[i] ); } } public void WaitForImage ( JApplet component, Image image ) { MediaTracker tracker = new MediaTracker ( component ); try { tracker.addImage ( image, 0 ); tracker.waitForID ( 0 ); } catch ( InterruptedException e ) { e.printStackTrace ( ); } } public Insets getInsets ( ) { return new Insets ( 5, 5, 5, 5 ); } class ImagePanel extends JPanel implements ActionListener, Runnable { private Thread runner; private boolean running, justonestep; public ImagePanel ( ) { justonestep = false; } public void paintComponent ( Graphics g ) { super.paintComponent ( g ); g.drawImage ( image[i], 0, 0, this ); } void startRunning() { if (running) return; runner = new Thread(this); running = true; runner.start(); } void stopRunning() { running = false; } public void run() { runit.setEnabled ( false ); stopit.setEnabled ( true ); while ( running ) { i++; if ( i == 12 ) i = 0; repaint ( ); if ( justonestep ) running = false; else { Thread.yield(); Pause ( pausetime ); } } justonestep = false; running = false; runit.setEnabled ( true ); stopit.setEnabled ( false ); } public void Pause ( int milli ) { try { Thread.sleep ( milli ); } catch ( InterruptedException e ) { } } public void actionPerformed ( ActionEvent evt ) { String command = evt.getActionCommand ( ); if ( command.equals ( "RUN / RESUME" ) ) startRunning ( ); else if ( command.equals ( "STOP" ) ) stopRunning ( ); else if ( command.equals ( "NEXT STEP" ) ) { justonestep = true; startRunning ( ); } else if ( evt.getSource ( ) == faster && pausetime > 50) { pausetime -= 100; numberField.setText ( "" + pausetime ); } else if ( evt.getSource ( ) == slower && pausetime < 1950) { pausetime += 100; numberField.setText ( "" + pausetime ); } } } }