Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
SimpleGameCanvas.java
Package: J2ME&Game.rar [view]
Upload User: gyyuli
Upload Date: 2013-07-09
Package Size: 3050k
Code Size: 2k
Category:
J2ME
Development Platform:
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);
- }
- }