HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"MIDlets: A Beginner's Guide to Writing Applications
for the MID Profile"
Vol. 6, Issue 7, p. 118

	


Listing 1

1.  import javax.microedition.midlet.MIDlet;
2.  import javax.microedition.lcdui.*;
3.
4.  public class HelloJDJMIDlet extends MIDlet {
5.
6.    private Display display;
7.    private TextBox text;
8.
9.    // provide a command to exit the app
10.   private Command bye = new Command("Exit", Command.SCREEN, 12);
11.   private CommandListener commandListener = new
                                        CommandListener() {
12.     public void commandAction(Command c, Displayable s) {
13.       if (c == bye) {
14.         destroyApp(true);
15.         notifyDestroyed();
16.         return;
17.       }
18.     }
19.   };
20.
21.   public HelloJDJMIDlet() {
22.     display = Display.getDisplay(this);
23.     text    = new TextBox("Simple MIDlet", "Hello JDJ!", 256, 0);
24.   }
25.
26.   public void startApp() {
27.     text.addCommand(bye);
28.     text.setCommandListener(commandListener);
29.
30.     display.setCurrent(text);
31.   }
32.
33.   public void pauseApp() {
34.   }
35.
36.   public void destroyApp(boolean unconditional) {
37.   }
38. }




Listing 2

1.  public void paint(Graphics g) {
2.    if (!running) {
3.      return;
4.    }
5.
6.    g.setColor(randRange(255), randRange(255), randRange(255));
7.
8.    triangle[0] = randRange(w);
9.    triangle[1] = randRange(h);
10.   triangle[2] = randRange(w);
11.   triangle[3] = randRange(h);
12.   triangle[4] = randRange(w);
13.   triangle[5] = randRange(h);
14.
15.   drawPoly(triangle,6,g);
16.
17. }



 Listing 3

1.    private final int randRange(int n) {
2.      int r = random.nextInt() % n;
3.      if (r < 0) {
4.        r += n;
5.      }
6.      return r;
7.    }
8.
9.
10.   private final void drawPoly(int coords[], int numcoords, Graphics g) {
11.
12.   ...
13.
14.   }



 Listing 4

1.  public void run() {
2.    int n = 0;
3.
4.    stopped = false;
5.
6.    while (!stopped) {
7.      now = System.currentTimeMillis();
8.      if (nextTime < now + 15)
9.        nextTime = now + 15;
10.     try {
11.       runner.sleep(nextTime - now);
12.     }
13.     catch(Exception e){}
14.     nextTime = System.currentTimeMillis() + 50;
15.
16.     screen.repaint();
17.
18.   }
19. }

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.