?? hellomidlet.java
字號:
package com.j2medev.chapter2;
import java.io.IOException;
import java.util.TimerTask;
import java.util.Timer;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet implements CommandListener {
private Display display = null;
private Command exitCommand = new Command("Exit",Command.EXIT,1);
public void startApp() {
//初始化display和MyCanvas對象
display = Display.getDisplay(this);
MyCanvas canvas = new MyCanvas();
canvas.addCommand(exitCommand);
canvas.setCommandListener(this);
display.setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command cmd,Displayable displayable){
if(cmd == exitCommand){
//退出應(yīng)用程序
destroyApp(false);
notifyDestroyed();
}
}
class MyCanvas extends Canvas{
private Timer timer = new Timer();//定時器
private int x = 10;
private int y = 10;
private int width = 0;//畫布的寬度
private int height = 0;//畫布的高度
private Image img = null;
public MyCanvas(){
width = getWidth();
height = getHeight();
try{
img = Image.createImage("/helloworld.png");
}catch(IOException ex){
ex.printStackTrace();
}
}
public void paint(Graphics g){
int color = g.getColor();
g.setColor(0xFFFFFF);
g.fillRect(0,0,width,height);
g.setColor(color);
g.drawImage(img,x,y,Graphics.LEFT|Graphics.TOP);
}
//當(dāng)Canvas顯示到當(dāng)前屏幕的時候此方法自動被調(diào)用
public void showNotify(){
//啟動定時器,每100毫秒執(zhí)行一次
timer.schedule(new TimerTask(){
private int dx = 2;
private int dy = 2;
public static final int SIGN = -1;
public void run(){
x = x + dx;
y = y + dy;
//判斷圖片是否到達屏幕邊界
if(x+2+img.getWidth() > width || x-2 < 0)
dx = SIGN*dx;
if(y + 2 +img.getHeight()> height || y-4 < 0)
dy = SIGN*dy;
repaint();
serviceRepaints();
}
},0,100);
}
//當(dāng)Canvas從當(dāng)前屏幕刪除的時候此方法被調(diào)用
public void hideNotify(){
timer.cancel();
img = null;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -