Java
  Home arrow Java arrow Page 2 - Getting Started with J2ME
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Getting Started with J2ME
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 32
    2005-04-05

    Table of Contents:
  • Getting Started with J2ME
  • Creating the “Hello, World” Application
  • Using KToolbar
  • Optimizing Your Game for Different Devices
  • Configuring the Server

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Getting Started with J2ME - Creating the “Hello, World” Application


    (Page 2 of 5 )

    This section shows the “Hello, World” application. The MIDlet will display the message Hello World! on the screen and remove it (or later put it back) when you click the Toggle Msg button. Clicking the Exit button will terminate the MIDlet. The application consists of two classes: the MIDlet subclass called Hello and the Canvas subclass called HelloCanvas. How it works is sufficiently simple that I’ll leave the explanations of the various steps in the comments. Chapter 2 includes in-depth discussion of how a MIDlet works. Listing 1-2 shows the code for Hello.java.

    Listing 1-2.Hello.java

      package net.frog_parrot.hello;
      import javax.microedition.midlet.*;
      import javax.microedition.lcdui.*;
     
    /**
      * This is the main class of the "Hello, World" demo.
      * 
      * @author Carol Hamer
     
    */
    public class Hello extends MIDlet implements CommandListener {
      /**
      * The canvas is the region of the screen that has been allotted 
      * to the game. 
      */
    HelloCanvas myCanvas;
      /**
      * The Command objects appear as buttons in this example.
      */
    private Command exitCommand = new Command("Exit", Command.EXIT, 99);
    /**
      * The Command objects appear as buttons in this example.
     
    */
    private Command toggleCommand = new Command("Toggle Msg", Command.SCREEN, 1);
    /**
      
    * Initialize the canvas and the commands.
      */
    public Hello() {
      myCanvas = new HelloCanvas(); 
      myCanvas.addCommand(exitCommand);
      myCanvas.addCommand(toggleCommand);
      // you set one command listener to listen to all
      // of the commands on the canvas:
     
      myCanvas.setCommandListener(this);
    }
    //---------------------------------------------------------// implementation of MIDlet
    /**
     
    * Start the application.
      */
    public void startApp() throws MIDletStateChangeException {
      // display my canvas on the screen:
      Display.getDisplay(this).setCurrent(myCanvas); 
      myCanvas.repaint();
    }
    /**
     
    * If the MIDlet was using resources, it should release 
      *
    them in this method.
      */
    public void destroyApp(boolean unconditional) 
        
    throws MIDletStateChangeException {
    }
    /**
      * This method is called to notify the MIDlet to enter a paused
      * state. The MIDlet should use this opportunity to release
      * shared resources.
     
    */
    public void pauseApp() {
    }
    //--------------------------------------------------------- // implementation of CommandListener
      /*
      * Respond to acommand issued on the Canvas. 
      * (either reset or exit).
      */
    public void commandAction(Command c, Displayable s) {
      if(c == toggleCommand) {
        myCanvas.toggleHello();
      } else if(c == exitCommand) {
        try {
          destroyApp(false);
          notifyDestroyed();
       
    } catch (MIDletStateChangeException ex){
        }
      }
     }
    }

    Listing 1-3 shows the code for HelloCanvas.java.

    Listing 1-3.HelloCanvas.java

    package net.frog_parrot.hello;

    import javax.microedition.lcdui.*;

    /**
      * This class represents the region of the screen that has been allotted
      * to the game.
      *
     
    * @author Carol Hamer
      */
    public class HelloCanvas extends Canvas {
      //-------------------------------------------------------  // fields
      /**
     
    * whether the screen should currently display the
      * "Hello World" message.
      */
    boolean mySayHello = true;
    //---------------------------------------------------------
    //    initialization and game state changes
    /**
      * toggle the hello message.
      */
    void toggleHello() {
      mySayHello = !mySayHello;
      repaint();
    }
    //---------------------------------------------------------
    // graphics methods
    /**
      * clear the screen and display the "Hello World" message if appropriate.
      */
    public void paint(Graphics g) {
      // get the dimensions of the screen:
      int width = getWidth ();
      int height = getHeight();
      // clear the screen (paint it white):
      g.setColor(0xffffff);
      // The first two args give the coordinates of the top
      // left corner of the rectangle. (0,0) corresponds
      // to the top-left corner of the screen.
      g.fillRect(0, 0, width, height);
      // display the "Hello World" message if appropriate:.
      if(mySayHello) {
       
    Font font = g.getFont();
        int fontHeight = font.getHeight();
        int fontWidth = font.stringWidth("Hello World!");
        // set the text color to red:
        g.setColor(255, 0, 0);
        g.setFont(font);
        // write the string in the center of the screen
       
    g.drawString("Hello World!", (width - fontWidth)/2,  
                      (height - fontHeight)/2,
                      g.TOP|g.LEFT);
       }
      }
    }

    The “Hello, World” application is simple enough to run with MIDP 1.0 as well as with MIDP 2.0. Figure 1-2 shows what the “Hello, World” application looks like when running on the Wireless Toolkit 1.0.4’s DefaultGrayPhone emulator. 


    Figure 1-2.  The “Hello, World” application

    More Java Articles
    More By Apress Publishing


       · I have seen many web-sites on java. I have been searching J2ME tutorials. Rest may...
     

    Buy this book now. This article is excerpted from J2ME Games with MIDP2 by Carol Hamer (Apress, 2004; ISBN 1590593820). Check it out at your favorite bookstore today. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...

    Iron Speed

    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway