001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.manyballs;
033:
034: import javax.microedition.lcdui.*;
035: import javax.microedition.midlet.MIDlet;
036: import javax.microedition.midlet.MIDletStateChangeException;
037:
038: public class ManyBalls extends MIDlet implements CommandListener {
039: Display display;
040: ManyCanvas canvas; // The main screen
041: private Command exitCommand = new Command("Exit", Command.EXIT, 99);
042: private Command toggleCommand = new Command("Stop/Go",
043: Command.SCREEN, 1);
044: private Command helpCommand = new Command("Help", Command.HELP, 2);
045: private Form helpScreen;
046:
047: // the GUI buttons
048: // Button exitButton, clearButton, moreButton, lessButton;
049:
050: /*
051: * Create the canvas
052: */
053: public ManyBalls() {
054: display = Display.getDisplay(this );
055:
056: canvas = new ManyCanvas(display, 40);
057: canvas.addCommand(exitCommand);
058: canvas.addCommand(toggleCommand);
059: canvas.addCommand(helpCommand);
060: canvas.setCommandListener(this );
061: }
062:
063: public void startApp() throws MIDletStateChangeException {
064: canvas.start();
065: }
066:
067: public void pauseApp() {
068: canvas.pause();
069: }
070:
071: public void destroyApp(boolean unconditional)
072: throws MIDletStateChangeException {
073: canvas.destroy();
074: }
075:
076: /*
077: * Respond to a command issued on the Canvas.
078: */
079: public void commandAction(Command c, Displayable s) {
080: if (c == toggleCommand) {
081: if (canvas.isPaused()) {
082: canvas.start();
083: } else {
084: canvas.pause();
085: }
086: } else if (c == helpCommand) {
087: canvas.pause();
088: showHelp();
089: } else if (c == exitCommand) {
090: try {
091: destroyApp(false);
092: notifyDestroyed();
093: } catch (MIDletStateChangeException ex) {
094: }
095: }
096: }
097:
098: /*
099: * Put up the help screen. Create it if necessary.
100: * Add only the Resume command.
101: */
102: void showHelp() {
103: if (helpScreen == null) {
104: helpScreen = new Form("Many Balls Help");
105: helpScreen.append("^ = faster\n");
106: helpScreen.append("v = slower\n");
107: helpScreen.append("< = fewer\n");
108: helpScreen.append("> = more\n");
109: }
110:
111: helpScreen.addCommand(toggleCommand);
112: helpScreen.setCommandListener(this);
113: display.setCurrent(helpScreen);
114: }
115: }
|