Simple Canvas : 2D « J2ME « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » J2ME » 2DScreenshots 
Simple Canvas
Simple Canvas

/*
Title:  J2ME Games With MIDP2
Authors:  Carol Hamer
Publisher:  Apress
ISBN:   1590593820
*/
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);
    // we 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 a command 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) {
      }
    }
  }
  
}



/**
 * This class represents the region of the screen that has been allotted 
 * to the game.
 
 @author Carol Hamer
 */
class HelloCanvas extends Canvas {

  //---------------------------------------------------------
  //   fields

  /**
   * whether or not 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(00, 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(25500);
      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);
    }
  }

}




           
       
Related examples in the same category
1. Simple Midlet DemoSimple Midlet Demo
2. Piano MIDletPiano MIDlet
3. PacerPacer
4. Pointer ExamplePointer Example
5. Text Example
6. Illustrate Graphics MIDletIllustrate Graphics MIDlet
7. Translate CoordinatesTranslate Coordinates
8. Key Canvas
9. Box Text CanvasBox Text Canvas
10. Offscreen MIDletOffscreen MIDlet
11. Quatsch MIDletQuatsch MIDlet
12. Sweep GameSweep Game
13. SweepSweep
14. Text MIDletText MIDlet
15. Translate coordinate systemTranslate coordinate system
16. Show various anchor pointsShow various anchor points
17. Draw mutable image on a canvas
18. Canvas for processing key code and commandsCanvas for processing key code and commands
19. Draw immutable image on a canvasDraw immutable image on a canvas
20. Use pointer events to draw onto the CanvasUse pointer events to draw onto the Canvas
21. A quick sample of graphics, commands, and event handling.A quick sample of graphics,  commands, and event handling.
w___w___w.___j__av__a__2___s__.c___o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.