www.pudn.com > resources.zip > HelloCanvas.java
package net.frog_parrot.hello;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
* This class is the display of the game.
*
* @author Carol Hamer
*/
public class HelloCanvas extends javax.microedition.lcdui.game.GameCanvas {
//---------------------------------------------------------
// fields
/**
* a handle to the display.
*/
Display myDisplay;
/**
* whether or not the screen should currently display the
* "hello world" message.
*/
boolean mySayHello = true;
//-----------------------------------------------------
// initialization and game state changes
/**
* Constructor merely sets the display.
*/
public HelloCanvas(Display d) {
super(false);
myDisplay = d;
}
/**
* This is called as soon as the application begins.
*/
void start() {
myDisplay.setCurrent(this);
repaint();
}
/**
* toggle the hello message.
*/
void newHello() {
mySayHello = !mySayHello;;
// paint the display
repaint();
}
//-------------------------------------------------------
// graphics methods
/**
* clear the screen and display the hello world message if appropriate.
*/
public void paint(Graphics g) {
int x = g.getClipX();
int y = g.getClipY();
int w = g.getClipWidth();
int h = g.getClipHeight();
// clear the screen:
g.setColor(0xffffff);
g.fillRect(x, y, w, h);
// display the hello world message if appropriate:.
if(mySayHello) {
Font dFont = g.getFont();
int fontH = dFont.getHeight();
int fontW = dFont.stringWidth("Hello World!");
g.setColor(0xffffff);
g.fillRect((w-fontW)/2 - 1, (h-fontH)/2,
fontW + 2, fontH);
g.setColor(0x00ff0000);
g.setFont(dFont);
g.drawString("Hello World!", (w-fontW)/2, (h-fontH)/2,
g.TOP|g.LEFT);
}
}
}