9. protected void keyPressed(int key)
{
switch ( getGameAction(key) ){
case Canvas.UP:
message = "up"; y--; break;
case Canvas.DOWN:
message = "down"; y++; break;
case Canvas.LEFT:
message = "left"; x--; break;
case Canvas.RIGHT:
message = "right"; x++; break;
case Canvas.FIRE:
message = "FIRE"; break;
}
repaint();
}
}
10. // A canvas that illustrates rectangles
class RectanglesCanvas extends Canvas {
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
// Create a white background
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
// Draw a solid rectangle
g.setColor(0);
g.drawRect(width/4, 0, width/2, height/4);
// Draw a dotted rectangle inside
// the solid rectangle.
g.setStrokeStyle(Graphics.DOTTED);
g.drawRect(width/4 + 4, 4, width/2 - 8, height/4 - 8);
// Draw a rounded rectangle
g.setStrokeStyle(Graphics.SOLID);
g.drawRoundRect(width/4, height/2, width/2, height/4, 16, 8);
}
}
11. // A canvas that illustrates filled rectangles
class RectangleFillsCanvas extends Canvas {
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
// Create a black background
g.setColor(0);
g.fillRect(0, 0, width, height);
// Draw a filled rectangle with
// a dotted rectangle around it
g.setStrokeStyle(Graphics.DOTTED);
g.setColor(0x00ff00);
g.fillRect(width/4, height/4, width/2, height/2);
g.setColor(0xffff00);
g.drawRect(width/4, height/4, width/2, height/2);
}
}
12. // A canvas that illustrates arcs
class ArcsCanvas extends Canvas {
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
// Create a black background
g.setColor(0);
g.fillRect(0, 0, width, height);
g.setColor(0xffffff);
g.drawArc(0, 0, width/2, height/2, 0, 90);
g.setStrokeStyle(Graphics.DOTTED);
g.setColor(0xffff00);
g.drawRect(0, 0, width/2, height/2);
g.setStrokeStyle(Graphics.SOLID);
g.setColor(0xffffff);
g.drawArc(width/2, 0, width/2, height/2, 0, -90);
g.setStrokeStyle(Graphics.DOTTED);
g.setColor(0xffff00);
g.drawRect(width/2, 0, width/2, height/2);
}
}
23. การทำางานของ
TimerTask
Main Time Task Display
ควบคุม TimerTask.run ทำาหน้าที่ Paint
Time.schedule()
(display) ตามคำาสั่ง random line
มีวิธีการเขียน code j2me แบบง่ายดังนี้
ครับ โดยเริ่มเขียนจาก
Java Class Java Class J2ME Midlet
extends Canvas extends TimerTask Class
26. import javax.microedition.midlet.*; TimerDemo.java
import javax.microedition.lcdui.*;
import java.util.*;
public class TimerDemo extends MIDlet {
Display display;
StarField field = new StarField();
FieldMover mover = new FieldMover();
Timer timer = new Timer();
public TimerDemo() {
display = Display.getDisplay( this );
}
protected void destroyApp( boolean unconditional ) { }
protected void startApp() {
display.setCurrent( field );
timer.schedule( mover, 0, 50 );
}
protected void pauseApp() { }
public void exit(){
timer.cancel(); // stop scrolling
destroyApp( true );
notifyDestroyed();
}
27. class FieldMover extends TimerTask { TimerDemo.java
public void run() {
field.scroll();
}
}
class StarField extends Canvas {
int height; int width; int[] stars;
Random generator = new Random();
boolean painting = false;
private Random genColor = new Random();
public StarField(){
height = getHeight();
width = getWidth();
stars = new int[ height ];
for( int i = 0; i < height; ++i ){
stars[i] = -1;
}
}
28. public void scroll() {
if( painting ) return;
for( int i = height-1; i > 0; --i ){
TimerDemo.java
stars[i] = stars[i-1];
}
stars[0] = ( generator.nextInt() % ( 3 * width ) ) / 2;
if( stars[0] >= width ){
stars[0] = -1;
}
repaint();
}
protected void paint( Graphics g ){
painting = true;
int r_color = genColor.nextInt(255);
int g_color = genColor.nextInt(255);
int b_color = genColor.nextInt(255);
g.setColor( 0, 0, 0 );
g.fillRect( 0, 0, width, height );
g.setColor( r_color, g_color, b_color );
for( int y = 0; y < height; ++y ){
int x = stars[y];
if( x == -1 ) continue;
g.drawLine( x, y, x, y );
}
painting = false;
}