GameActionDemo.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:3k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- public class GameActionDemo extends MIDlet {
- private Display display;
- public GameActionDemo() {
- display=Display.getDisplay(this);
- }
- public void startApp() throws MIDletStateChangeException {
- display.setCurrent(new GameActionDemoCanvas());
- }
- /**
- * Pause the MIDlet
- */
- public void pauseApp() {
- }
- /**
- * Called by the framework before the application is unloaded
- */
- public void destroyApp(boolean unconditional) {
- }
- class GameActionDemoCanvas extends Canvas {
- int width, height;
- int deltaX, deltaY;
- int x,y;
- public GameActionDemoCanvas() {
- //draw a 8x10 net
- deltaX=this.getWidth()/8;
- deltaY=this.getHeight()/10;
- width=deltaX*8;
- height=deltaY*10;
- x=0;
- y=0;
- }
- public void paint(Graphics g) {
- //set background to white
- g.setColor(0xFFFFFF);
- g.fillRect(0,0,width,height);
- //set foreground color to black
- g.setColor(0x000000);
- g.fillRect(x,y,deltaX,deltaY);
- }
- public void keyPressed(int keycode) {
- switch(getGameAction(keycode)) {
- case Canvas.DOWN:
- y+=deltaY;
- if(y>=height) {
- y-=height;
- repaint(x,0,deltaX,height);
- }
- else {
- repaint(x,y-deltaY,deltaX,2*deltaY);
- }
- break;
- case Canvas.UP:
- y-=deltaY;
- if(y<0) {
- y+=height;
- repaint(x,0,deltaX,height);
- }
- else {
- repaint(x,y,deltaX,2*deltaY);
- }
- break;
- case Canvas.LEFT:
- x-=deltaX;
- if(x<0) {
- x+=width;
- repaint(0,y,width,deltaY);
- }
- else {
- repaint(x,y,deltaX*2,deltaY);
- }
- break;
- case Canvas.RIGHT:
- x+=deltaX;
- if(x>=width) {
- x-=width;
- repaint(0,y,width,deltaY);
- }
- else {
- repaint(x-deltaX,y,deltaX*2,deltaY);
- }
- break;
- default:
- }
- }
- }
- }