/* * Bouncing balls - each ball is a different thread with a different * time to sleep. * Adds pause and continue buttons. */ import java.awt.*; import java.awt.event.*; public class BouncePause extends Frame implements ActionListener { private Canvas canvas; // store each thread in an array as it is created private int maxthreads = 100; private Thread[] threadarray = new Thread[maxthreads]; private int lastthread = -1; // the constructor initializes the frame to be a borderlayout // with a canvas in the center and // four buttons labelled Start, Pause, Continue, and Close in the south public BouncePause() { setLayout(new BorderLayout()); setTitle("BouncePause"); canvas = new Canvas(); add(canvas, BorderLayout.CENTER); Panel p = new Panel(); Button b1 = new Button("Start"); b1.addActionListener(this); p.add(b1); Button b3 = new Button("Pause"); b3.addActionListener(this); p.add(b3); Button b4 = new Button("Continue"); b4.addActionListener(this); p.add(b4); Button b2 = new Button("Close"); b2.addActionListener(this); p.add(b2); add(p, BorderLayout.SOUTH); } // responds to the Start button by creating a new instance // of the BallTimes class and starting its thread, // stores each thread in an array of threads // responds to the Pause button by calling thread method suspend // on all the threads // responds to the Continue button by calling thread method resume // on all the threads // responds to the Close button by exiting the application public void actionPerformed(ActionEvent evt) { if (evt.getActionCommand().equals("Start")) { if (lastthread < (maxthreads - 1)) { lastthread++; BallTimes b = new BallTimes(canvas); threadarray[lastthread] = b; b.start(); } } else if (evt.getActionCommand().equals("Pause")) { for (int i = 0; i <= lastthread; i++) threadarray[i].suspend(); } else if (evt.getActionCommand().equals("Continue")) { for (int i = 0; i <= lastthread; i++) threadarray[i].resume(); } else if (evt.getActionCommand().equals("Close")) System.exit(0); } // the main method creates an instance of this class and sets the // properties of the frame public static void main(String[] args) { Frame f = new BouncePause(); f.setSize(400, 300); f.setVisible(true); } } /* The BallTimes class is a subclass of Thread, so each instance inherits * thread methods start, stop, sleep, etc. * The run method of the thread loops to draw a ball in a new location * for a fixed number of iterations. * This example has an additional variable sleeptime, which is used to * sleep inbetween iterations. * Note that this class is unchanged from BounceTimes.java example. */ class BallTimes extends Thread { private Canvas box; private static final int XSIZE = 10; private static final int YSIZE = 10; private int x = 0; private int y = 0; private int dx = 2; private int dy = 2; private int sleeptime; // the constructor initializes the canvas to that of the frame // and initializes sleeptime to a random number between 4 and 44 public BallTimes(Canvas c) { box = c; sleeptime = ((int)(Math.random() * 40)) + 4; } // draws the ball in the current location public void draw() { Graphics g = box.getGraphics(); g.fillOval(x, y, XSIZE, YSIZE); g.dispose(); } // increments the location of the ball, checks the boundary to see // if it should "bounce", and draws the ball public void move() { Graphics g = box.getGraphics(); g.setXORMode(box.getBackground()); g.fillOval(x, y, XSIZE, YSIZE); x += dx; y += dy; Dimension d = box.getSize(); 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(); } // loops for 1000 times and moves the ball public void run() { draw(); for (int i = 1; i <= 1000; i++) { move(); try { Thread.sleep(sleeptime); } catch(InterruptedException e) {} } } }