/* * BounceTempate * This program template is set up to have a continuous painting of an animation is the * canvas part of the applet. * The main applet implements a set of control components at the bottom of the canvas. */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Bounce extends Applet implements ActionListener { Drawarea drawing = new Drawarea ( ); Panel controls = new Panel ( ); Button pause, resume; // add variables for additional panels and components here public void init ( ) { // create components and add them to panels, add event listeners, // including the pause and resume buttons // create the control panel // set the layout and contents of the whole applet setLayout ( new BorderLayout ( ) ); add ( drawing, BorderLayout.CENTER ); add ( controls, BorderLayout.SOUTH ); } // the following three methods can be used without changes to start and // stop the thread of the canvas, and to paint the canvas // the applet start method public void start() { drawing.start ( ); } // the applet stop method public void stop( ) { drawing.stop ( ); } // the applet paint method public void paint ( Graphics g ) { drawing.paint ( g ); } // method to respond to action events public void actionPerformed ( ActionEvent event ) { // respond to other buttons here. // respond to pause button by calling the thread suspend method if ( event.getSource ( ) == pause ) { drawing.thread.suspend ( ) ; } // respond to resume button by calling the thread resume method if ( event.getSource ( ) == resume ) { drawing.thread.resume ( ) ; } } } // end of applet class Drawarea extends Canvas implements Runnable { // variables that are properties of the drawing // include these variables without change Thread thread; Image buffer; // variables for double buffering: a separate image used as a canvas Graphics gbuffer; // and its graphics public Drawarea ( ) { // initialization (except for values that depend on the size of the canvas) } // use these two methods without change public void start() { // create a thread and start it running if ( thread == null ) { thread = new Thread( this ); thread.start(); } } public void stop() { // stop the thread from running and destroy it if ( thread != null ) { thread.stop(); thread = null; } } // this run method is what the thread executes public void run() { // continuous loop while ( thread != null ) { repaint(); try // wait for 20 milliseconds before continuing to repaint { Thread.sleep(20); } catch ( InterruptedException e ) { }; } } // give an update that does not blank out background (which would cause flicker) public void update (Graphics g) { paint (g); } public void paint ( Graphics g ) { // the first time, do initialization that depends on the size of the canvas if ( buffer == null ) { // Create an off-screen image for double buffering: buffer = createImage( getSize( ).width, getSize( ).height ); // Get off-screen graphics context: gbuffer = buffer.getGraphics(); } // all drawing should take place on gbuffer // draw background to cover up previous image gbuffer.setColor ( getBackground ( ) ); gbuffer.fillRect ( 0, 0, getSize( ).width , getSize( ).height ); // draw everything using gbuffer here, including setColor // draw the buffer in the canvas drawarea by using graphics g // this makes the drawing appear in the applet all at once g.drawImage( buffer, 0, 0, this ); } }