/* * @(#)ManyCanvas.java 1.8 00/08/03 * Copyright (c) 1999,2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ package example.manyballs; import javax.microedition.lcdui.*; public class ManyCanvas extends javax.microedition.lcdui.Canvas { Display display; // a set of free roaming balls SmallBall[] balls; int numBalls; int width, height; boolean paused; public ManyCanvas(Display d, int maxBalls) { display = d; // save the display // initialize the array of balls balls = new SmallBall[maxBalls]; width = getWidth(); height = getHeight(); // Start with one ball balls[0] = new SmallBall(this, 0, 0,width,height-12); numBalls = 1; paused = true; } /** * Draws the drawing frame (which also contains the ball) and the * controls. */ protected void paint(Graphics g) { // Draw the frame g.setColor(0xffffff); g.fillRect(0,0, width-1, height-1); g.setColor(0); g.drawRect(0+1,0+1,width-2,height-2); // Draw each ball for(int i = 0; i < numBalls; i++) { balls[i].paint(g); } g.drawString(numBalls + " Balls", 5, height-14, 0); } /** * Handle a pen down event. */ public void keyPressed( int keyCode) { int action = getGameAction(keyCode); switch (action) { case LEFT: // Reduce the number of threads if (numBalls > 0) { // decrement the counter numBalls = numBalls - 1; // stop the thread and remove the reference to it balls[numBalls].stop = true; balls[numBalls] = null; } break; case RIGHT: // Increase the number of threads if (numBalls < balls.length) { // create a new ball and start it moving balls[numBalls] = new SmallBall(this, 0, 0, width,height-12); new Thread(balls[numBalls]).start(); // increment the counter numBalls = numBalls + 1; } break; case UP: // Make them move faster SmallBall.faster(); break; case DOWN: // Make them move slower SmallBall.slower(); break; } repaint(); } /** * Destroy */ void destroy() { // kill all the balls and terminate for (int i = 0; i < balls.length && balls[i] != null; i++) { balls[i].stop = true; // enable the balls to be garbage collected balls[i] = null; } numBalls = 0; } /* * Return whether the canvas is paused or not. */ boolean isPaused() { return paused; } /** * Pause the balls by signaling each of them to stop. * The ball object still exists and holds the current position * of the ball. It may be restarted later. * The thread will terminate. * TBD: is a join needed? */ void pause() { if (!paused) { paused = true; for (int i = 0; i < balls.length && balls[i] != null; i++) { balls[i].stop = true; } } repaint(); } /* * Start creates a new thread for each ball and start it. */ void start() { if (paused) { paused = false; display.setCurrent(this); for (int i = 0; i < balls.length && balls[i] != null; i++) { Thread t = new Thread(balls[i]); t.start(); } } repaint(); } }