📄 playcanvas.java
字号:
package Petgame;
import javax.microedition.lcdui.*;
public class PlayCanvas extends Canvas implements Runnable {
/** Constructor */
private Image pic;
private Thread thread;
private boolean run = false;
public PlayCanvas() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
petinit();
thread = new Thread(this);
thread.start();
}
private Pet pet;
/** Component initialization */
private void jbInit() throws Exception {
// Set up this Displayable to listen to command events
setCommandListener(new PlayCanvas_CommandAdapter(this));
// add the Exit command
addCommand(new Command("Exit", Command.EXIT, 1));
addCommand(new Command("Start", Command.OK, 1));
}
private void petinit() {
try {
pic = Image.createImage("/Petgame/pet.png");
} catch (Exception e) {
e.printStackTrace();
}
pet = new Pet(pic, 110, 115);
pet.setpos(30, 30);
}
/** Handle command events */
public void this_commandPerformed(Command command, Displayable displayable) {
/** @todo Add command handling code */
if (command.getCommandType() == Command.EXIT) {
// stop the MIDlet
Petgame.quitApp();
} else if (command.getCommandType() == Command.OK) {
// stop the MIDlet
run = true;
}
}
/** Required paint implementation */
public void run() {
while (true) {
try {
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
protected void paint(Graphics g) {
/** @todo Add paint codes */
if (run) {
g.setColor(255,255,255);
g.fillRect(0,0,g.getClipHeight(),g.getClipWidth());
g.setColor(0,0,0);
pet.paint(g);
pet.drawMsg(g);
pet.playing();
}
}
protected void keyPressed(int keyCode) {
int action = getGameAction(keyCode); /* 僉乕偺抣撉傒庢傝 */
if (action == Canvas.LEFT) {
pet.dining();
} else if (action == Canvas.RIGHT) {
pet.playgame();
} else if (action == Canvas.UP) {
pet.repose();
} else if (action == Canvas.DOWN) {
pet.nextframe();
}
}
class PlayCanvas_CommandAdapter implements CommandListener {
private PlayCanvas instance;
public PlayCanvas_CommandAdapter(PlayCanvas instance) {
this.instance = instance;
}
public void commandAction(Command command, Displayable displayable) {
instance.this_commandPerformed(command, displayable);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -