/* PingPongApplet: example of input from text field and button components in an applet which animates. -bds 10/98 All println's are for debugging only. */ import java.applet.*; import java.awt.*; import java.awt.event.*; // Applet handles user interface // A second thread named PingPongBall handles animation (and sleeps a lot). // Use of this thread in the applet is denoted (at line end) by ////// public class PingPongApplet extends Applet implements ActionListener { // methods defined are init, paint, actionPerformed. private Dimension size; private PingPongBall animator = new PingPongBall( this ); ////// private TextField distance_source = new TextField( 12 ); private Button on_off = new Button( "on/off" ); // set the relations among components, threads. public void init() { this. add( distance_source ); // Put the TextField in this applet. // Tell it who handles the event that text is entered in the field. distance_source.addActionListener( this ); distance_source.setText( "distance please" ); // just an initial val this. add( on_off ); // put the Button in this applet // Tell it who handles mouse click events on the button. on_off. addActionListener( this ); setBackground( Color.white ); running = true; animator.start(); ////// } // method init // Draw a ping pong table. public void paint( Graphics page ) { size = getSize(); // could have changed // Draw the table. page.setColor( Color.red ); page.drawRect( size.width/10, size.height/10, 8*size.width/10, 8*size.height/10); page.drawLine(size.width/2, size.height/10, size.width/2, 9*size.height/10); System.out.println(" Paint: drew table"); } // method paint private boolean running; // to know whether to suspend or resume action. // Handle the on_off or distance_source events. public void actionPerformed( ActionEvent event ) { System.out.println( event.toString() ); // just curious String cmd = event.getActionCommand(); if ( cmd.equals( "on/off" ) ) { // Toggle the animation on or off. if ( running ) animator.suspend(); ////// else animator.resume(); ////// running = !running; // if we was, we aint; if we warnt, we is. } else { // Use textfield input to set the distance the ball moves. animator.setDistance( Integer.parseInt( cmd ) ); ////// } } // method actionPerformed } // class PingPongApplet ////// continuation of file PingPongApplet.java ////////////////////////////// // PingPongBall draws itself in various positions on an associated applet. class PingPongBall extends Thread { // methods defined are: run, paintBall, setDistance, setPositionData. private Point center = new Point(); // center of window private int diameter; // of the ping pong ball private int distance = 1; // of ball from center of table, unit is diameters. private PingPongApplet applet; // for whom I Animate private Graphics page; // And the applet's graphics for me to write on. PingPongBall() { System.out.println("PingPongBall() constructor called "); } PingPongBall( PingPongApplet whoIAnimate ) { applet = whoIAnimate; System.out.println("PingPongBall(PingPongApplet) constructor called"); } public void run() { System.out.println("PingPongBall.run() called"); page = applet.getGraphics(); page.setColor( Color.white ); page.setXORMode( Color.green ); int i = 1; Sys.snooze(100); // let applet get ready (..there is a better way) while (true) { setPositionData(applet.getSize()); // could change any second. //draw int j = i*distance; paintBall( j ); Sys.snooze(1000); paintBall( j ); i = (i == 1)? -1 : 1; // alternates 1, -1, 1, -1, 1, ... } } // method run private void paintBall( int d ) { page.fillOval( center.x + d*diameter, center.y - diameter/2, diameter, diameter ); } public void setDistance( int distance ) { this.distance = distance; } private void setPositionData(Dimension size) { diameter = (size.width + size.height)/30; center.x = (size.width-diameter)/2; center.y = size.height/2; } // method setPositionData } // class PingPongBall /* */