SimpleGameCanvas.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:
J2ME
开发平台:
Java
- // Any use of this code and/or information below is subject to the
- // license terms: http://wireless.java.sun.com/berkeley_license.html
- import javax.microedition.lcdui.*;
- import javax.microedition.lcdui.game.*;
- public class SimpleGameCanvas extends GameCanvas implements Runnable
- {
- private Graphics g;
- private int[ ] clrs = {0X0000FFFF,0x00FF0000,0x0000FF00,0x000000FF};
- private int curColor = 0X000000FF;
- private int[ ] rgb;
- private int curX = 0,curY = 0;
- private int maxX, maxY;
- private boolean mTrucking;
- private long mFrameDelay;
- public SimpleGameCanvas() {
- super(true);
- //初始化用于Alpha 绘图的像素数组
- rgb = new int[81];
- for(int i = 0;i<81;i++)
- rgb[i] = 0x88FFFFFF;
- g = getGraphics( );
- //计算方块移动范围
- maxX = getWidth( ) -9;
- maxY = getHeight( ) -9;
- }
- public void start() {
- mTrucking = true;
- Thread t = new Thread(this);
- t.start();
- }
- public void stop() { mTrucking = false; }
- public void run() {
- Graphics g = getGraphics();
- while (mTrucking == true) {
- tick();
- input();
- render(g);
- try { Thread.sleep(mFrameDelay); }
- catch (InterruptedException ie) {}
- }
- }
- private void tick() {
- }
- private void input() {
- int keyState = getKeyStates( );
- //判断ABCD键是否被按下
- if((keyState & GAME_A_PRESSED) != 0)
- curColor=clrs[0];
- else if((keyState & GAME_B_PRESSED) != 0)
- curColor=clrs[1];
- else if((keyState & GAME_C_PRESSED) != 0)
- curColor=clrs[2];
- else if((keyState & GAME_D_PRESSED) != 0)
- curColor=clrs[3];
- //判断上下左右键是否被按下
- if((keyState & UP_PRESSED) != 0 && curY>0)
- curY --;
- if((keyState & DOWN_PRESSED) != 0 && curY<maxY)
- curY ++;
- if((keyState & LEFT_PRESSED) != 0 && curX>0)
- curX --;
- if((keyState & RIGHT_PRESSED) != 0 && curX<maxX)
- curX ++;
- }
- private void render(Graphics g) {
- g.setColor(curColor);
- g.fillRect(curX,curY,9,9);
- flushGraphics( );
- //通过Alpha 混合使原来的方块颜色变浅
- g.drawRGB(rgb,0,9,curX,curY,9,9,true);
- }
- }