ITPub博客

首页 > 应用开发 > IT综合 > 手机上画图的程序代码

手机上画图的程序代码

原创 IT综合 作者:lbkfindok 时间:2005-07-28 17:19:27 0 删除 编辑

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/*
* 创建日期 2005-7-24
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/

/**
* @author LBK
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class GameCanvasTest extends MIDlet
{


/**
*
*/
public GameCanvasTest()
{
super();
// TODO 自动生成构造函数存根
}

/* (非 Javadoc)
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException
{
// TODO 自动生成方法存根
MyGameCanvas canvas = new MyGameCanvas();

Display.getDisplay(this).setCurrent(canvas);
Timer t = new Timer();
MyTask task = new MyTask(canvas);
t.schedule(task,0,50); //执行任务

}

/* (非 Javadoc)
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp()
{
// TODO 自动生成方法存根

}

/* (非 Javadoc)
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
// TODO 自动生成方法存根

}


}

class MyGameCanvas extends GameCanvas
{
private Graphics g;
private int[] clrs = {0x0000001F,0x00FF0000,0X0000FF00,0x000000FF};
private int curColor = clrs[0];
private int currentX = 0, currentY = 0;
private int maxX,maxY;
public MyGameCanvas()
{
super(true);
g = getGraphics();
//横坐标和纵坐标的最大值
maxX = getWidth()-3;
maxY = getHeight()-3;
}

public void Action()
{
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 && currentY > 0)
currentY--;
if((keyState & DOWN_PRESSED)!=0 && currentY < maxY)
currentY++;
if((keyState & LEFT_PRESSED)!=0 && currentX > 0)
currentX--;
if((keyState & RIGHT_PRESSED)!=0 && currentX < maxX)
currentX++;

//绘制新的方块
g.setColor(curColor);
g.fillRect(currentX,currentY,3,3);
flushGraphics();

}
}

class MyTask extends TimerTask
{
private MyGameCanvas canvas;

public MyTask(MyGameCanvas myGameCanvas)
{
canvas = myGameCanvas;
}

public void run()
{
canvas.Action();
}
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/314771/viewspace-803280/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 网络
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2008-07-24

  • 博文量
    3
  • 访问量
    5049