1 /* 2 * This application extends the previous BounceThread so that 3 * ach ball is a different thread with a different time to sleep. 4 * 5 */ 6 7 import java.awt.*; 8 import java.awt.event.*; 9 10 public class BounceTimes extends Frame implements ActionListener 11 { 12 private Canvas canvas; 13 14 // the constructor initializes the frame to be a borderlayout 15 // with a canvas in the center and 16 // two buttons labelled Start and Close in the south 17 18 public BounceTimes() 19 { setLayout(new BorderLayout()); 20 setTitle("BounceTimes"); 21 22 canvas = new Canvas(); 23 add(canvas, BorderLayout.CENTER); 24 25 Panel p = new Panel(); 26 Button b1 = new Button("Start"); 27 b1.addActionListener(this); 28 p.add(b1); 29 Button b2 = new Button("Close"); 30 b2.addActionListener(this); 31 p.add(b2); 32 add(p, BorderLayout.SOUTH); 33 } 34 35 // responds to the Start button by creating a new instance 36 // of the BallTimes class and starting its thread 37 // responds to the close button by exiting the application 38 39 public void actionPerformed(ActionEvent evt) 40 { if (evt.getActionCommand().equals("Start")) 41 { BallTimes b = new BallTimes(canvas); 42 b.start(); 43 } 44 else if (evt.getActionCommand().equals("Close")) 45 System.exit(0); 46 } 47 48 // the main method creates an instance of this class and sets the 49 // properties of the frame 50 51 public static void main(String[] args) 52 { Frame f = new BounceTimes(); 53 f.setSize(400, 300); 54 f.setVisible(true); 55 } 56 57 } 58 59 /* The BallTimes class is a subclass of Thread, so each instance inherits 60 * thread methods start, stop, sleep, etc. 61 * The run method of the thread loops to draw a ball in a new location 62 * for a fixed number of iterations. 63 * This example has an additional variable sleeptime, which is used to 64 * sleep inbetween iterations. 65 */ 66 67 class BallTimes extends Thread 68 { 69 private Canvas box; 70 private static final int XSIZE = 10; 71 private static final int YSIZE = 10; 72 private int x = 0; 73 private int y = 0; 74 private int dx = 2; 75 private int dy = 2; 76 private int sleeptime; 77 78 // the constructor initializes the canvas to that of the frame 79 // and initializes sleeptime to a random number between 4 and 44 80 public BallTimes(Canvas c) 81 { box = c; 82 sleeptime = ((int)(Math.random() * 40)) + 4; 83 } 84 85 // draws the ball in the current location 86 public void draw() 87 { Graphics g = box.getGraphics(); 88 g.fillOval(x, y, XSIZE, YSIZE); 89 g.dispose(); 90 } 91 92 // increments the location of the ball, checks the boundary to see 93 // if it should "bounce", and draws the ball 94 public void move() 95 { Graphics g = box.getGraphics(); 96 g.setXORMode(box.getBackground()); 97 g.fillOval(x, y, XSIZE, YSIZE); 98 x += dx; 99 y += dy; 100 Dimension d = box.getSize(); 101 if (x < 0) { x = 0; dx = -dx; } 102 if (x + XSIZE >= d.width) { x = d.width - XSIZE; dx = -dx; } 103 if (y < 0) { y = 0; dy = -dy; } 104 if (y + YSIZE >= d.height) { y = d.height - YSIZE; dy = -dy; } 105 g.fillOval(x, y, XSIZE, YSIZE); 106 g.dispose(); 107 } 108 109 // loops for 1000 times and moves the ball 110 public void run() 111 { draw(); 112 for (int i = 1; i <= 1000; i++) 113 { move(); 114 try { Thread.sleep(sleeptime); } catch(InterruptedException e) {} 115 } 116 } 117 } 118 119 120