Subscribe to RSS Feed

Jumat, 08 Juni 2012

Tulisan 12: Contoh Program Canvas Pada J2ME

CANVAS

import javax.microedition.lcdui.*;

public class KeyCanvas extends Canvas {
private Font mFont;
private String mMessage = "[Press keys]";
private int x=41;
private int y=115;
private static int w,h;

public KeyCanvas() {
mFont = Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
}

public void paint(Graphics g) {
w = getWidth();
h = getHeight();

// Clear the Canvas.
g.setGrayScale(255);
g.fillRect(0, 0, w - 1, h - 1);
g.setGrayScale(0);
g.drawRect(0, 0, w - 1, h - 1);

g.setFont(mFont);

g.setColor(0,0,200);
g.fillRect(0,0,w,h);
g.setColor(255,255,255);
g.drawRoundRect(x,y,150,70,40,40);
g.drawString("Tugas J2ME Key Canvas",(x+17),(y+28),g.TOP|g.LEFT);
}

protected void keyPressed(int keyCode) {

int gameAction = getGameAction(keyCode);
switch(gameAction) {
case UP:
y=y-8;
if(y<=0)
y=0;
break;
case DOWN:
y=y+8;
if(y>=218)
y=218;
break;
case LEFT:
x=x-8;
if(x<=0)
x=0;
break;
case RIGHT:
x=x+8;
if(x>=89)
x=89;
break;
}
repaint();
}
}

0 komentar:

Posting Komentar