main.java
来自「j2me手机应用开发的例子程序」· Java 代码 · 共 80 行
JAVA
80 行
package test;
import javax.microedition.lcdui.Canvas;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
public class Main extends MIDlet {
Canvas myCanvas;
public Main() {
myCanvas = new TestMCAPI(this);
}
public void startApp() {
Display display = Display.getDisplay(this);
// remember, Canvas is a Displayable so it can
// be set on the display like Screen elements
display.setCurrent(myCanvas);
// force repaint of the canvas
myCanvas.repaint();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
class MyCanvas extends Canvas {
public void paint(Graphics g) {
// create a 20x20 black square in the center
// clear the screen first
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x000000); // Clip is black
g.drawRect(g.getClipX()+1, g.getClipX()+1, g.getClipWidth()-3, g.getClipHeight()-3);
// draw the square, changed to rely on instance variables
g.setColor(0x0000ff); // cube is blue
g.fillRect(x, y, 40, 40);
}
public void keyPressed(int keyCode) {
// what game action does this key map to?
int gameAction = getGameAction(keyCode);
if(gameAction == RIGHT) {
x += dx;
} else if(gameAction == LEFT) {
x -= dx;
} else if(gameAction == UP) {
y -= dy;
} else if(gameAction == DOWN) {
y += dy;
} else if(gameAction == FIRE) {
//getMidlet().notifyDestroyed();
}
// make sure to repaint
repaint(0,0,130,130);
}
// starting coordinates
private int x = getWidth()/2 - 10;
private int y = getHeight()/2 - 10;
// distance to move
private int dx = 2;
private int dy = 2;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?