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