/* file BncExprApp.java ... Hacked by RRS*/ import java.awt.*; import java.applet.*; public class BncExprApp extends Applet { Canvas canvas; boolean live = true; public void init( ) { setLayout( new BorderLayout( ) ); resize( 300, 200 ); show( ); } public void start( ) { //setTitle( "BncExprApp" ); live = true; canvas = new Canvas( ); add( "Center", canvas ); Panel p = new Panel( ); p.add( new Button( "Start" ) ); p.add( new Button( "Express" ) ); p.add( new Button( "Quit" ) ); add( "South", p); } public boolean handleEvent( Event evt ) { if ( evt.id == Event.WINDOW_DESTROY ) { live = false; stop();} return super.handleEvent( evt ); } public boolean action( Event evt, Object arg ) { if( arg.equals( "Start" ) ) { live = true; Ball b = new Ball( canvas, Color.black, this); b.setPriority( Thread.NORM_PRIORITY); b.start( ); } else if (arg.equals( "Express" ) ) { live = true; Ball b = new Ball( canvas, Color.red, this); b.setPriority( Thread.NORM_PRIORITY + 2); b.start( ); } else if (arg.equals( "Quit" ) ) { //Thread.currentThread().stop(); Event e1 = new Event( this, Event.WINDOW_DESTROY, "Quit" ); live = false; stop(); return super.handleEvent( e1); } else return super.action( evt, arg); return true; } } class Ball extends Thread { BncExprApp daddy; Canvas box; Color color; private static final int XSIZE = 10, YSIZE = 10; private int x = 0, y = 0, dx = 2, dy = 2; Ball ( Canvas c, Color co, BncExprApp parent ) { box = c; color = co; daddy = parent; } public void draw( ) { Graphics g = box.getGraphics( ); g.setColor( color ); g.setXORMode( box.getBackground( )); g.fillOval( x, y, XSIZE, YSIZE ); g.dispose( ); } public void move( ) { Graphics g = box.getGraphics( ); g.setColor( color ); g.setXORMode( box.getBackground( )); g.fillOval( x, y, XSIZE, YSIZE ); x += dx; y += dy; Dimension d = box.size( ); if ( x < 0 ) { x = 0; dx = -dx; } if ( x + XSIZE >= d.width) { x = d.width - XSIZE; dx = -dx; } if ( y < 0 ) { y = 0; dy = -dy; } if ( y + YSIZE >= d.height) { y = d.height - YSIZE; dy = -dy; } g.fillOval( x, y, XSIZE, YSIZE ); g.dispose( ); } public void run( ) { draw( ); for( int i =1; i < 1000; i++ ) { if ( !daddy.live ) break; move( ); try { sleep( 5 ); } catch ( InterruptedException e) { }; } draw( ); } }