001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.demos.manyballs;
028:
029: import javax.microedition.midlet.*;
030: import javax.microedition.lcdui.*;
031:
032: public class ManyBalls extends MIDlet implements CommandListener {
033:
034: Display display;
035: ManyCanvas canvas; // The main screen
036: private Command exitCommand = new Command("Exit", Command.EXIT, 99);
037: private Command toggleCommand = new Command("Stop/Go",
038: Command.SCREEN, 1);
039: private Command helpCommand = new Command("Help", Command.HELP, 2);
040: private Form helpScreen;
041: private String helpText = "^ = faster\n v = slower\n < = fewer\n> = more";
042:
043: // the GUI buttons
044: // Button exitButton, clearButton, moreButton, lessButton;
045:
046: /*
047: * Create the canvas
048: */
049: public ManyBalls() {
050: display = Display.getDisplay(this );
051:
052: canvas = new ManyCanvas(display, 40);
053: canvas.addCommand(exitCommand);
054: canvas.addCommand(toggleCommand);
055: canvas.addCommand(helpCommand);
056: canvas.setCommandListener(this );
057: }
058:
059: public void startApp() throws MIDletStateChangeException {
060: canvas.start();
061: }
062:
063: public void pauseApp() {
064: canvas.pause();
065: }
066:
067: public void destroyApp(boolean unconditional)
068: throws MIDletStateChangeException {
069: canvas.destroy();
070: }
071:
072: /*
073: * Respond to a command issued on the Canvas.
074: */
075: public void commandAction(Command c, Displayable s) {
076: if (c == toggleCommand) {
077: if (canvas.isPaused())
078: canvas.start();
079: else
080: canvas.pause();
081: } else if (c == helpCommand) {
082: canvas.pause();
083: showHelp();
084: } else if (c == exitCommand) {
085: try {
086: destroyApp(false);
087: notifyDestroyed();
088: } catch (MIDletStateChangeException ex) {
089: }
090: }
091: }
092:
093: /*
094: * Put up the help screen. Create it if necessary.
095: * Add only the Resume command.
096: */
097: void showHelp() {
098: if (helpScreen == null) {
099: helpScreen = new Form("Many Balls Help");
100: helpScreen.append("^ = faster\n");
101: helpScreen.append("v = slower\n");
102: helpScreen.append("< = fewer\n");
103: helpScreen.append("> = more\n");
104: }
105: helpScreen.addCommand(toggleCommand);
106: helpScreen.setCommandListener(this);
107: display.setCurrent(helpScreen);
108: }
109:
110: }
|