import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
//made by sword2008·程序之巢
//游戏第一课 画格子
public class Gofirst1 extends MIDlet
{
public Gofirst1(){
}
public void startApp(){
Display.getDisplay(this).setCurrent(new First1());
}
public void pauseApp(){}
public void destroyApp(boolean unkown){}
}
class First1 extends Canvas
{ private int color;
private int forOneTime=0;
private Graphics g;
private int startX=getWidth()/4-40;//起点坐标和终点坐标非常重要,格子剩下的是分成等分
private int startY=getHeight()/4-40;
private int nowX=startX,nowY=startY;
public void paint(Graphics g){
if(forOneTime==0){
color=g.getColor();
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,0,0);
g.drawRect(startX,startY,200,200);
drawline(g);
}
else if(forOneTime==1){//fire
g.setColor(0,255,0);
g.fillRect(nowX,nowY,20,20);
long memory = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
System.gc();
System.out.println("当前占用内存:"+memory);}//fillRect()(起点坐标,终点坐标,横坐标,竖坐标)
else if(forOneTime==2){//down
nowY+=20;//竖坐标加20,以下依此类推
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
}
else if(forOneTime==3){//up
nowY-=20;
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
}
else if(forOneTime==4){//left
nowX-=20;
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
}
else if(forOneTime==5){//right
nowX+=20;
ifmaxmingolong();
g.fillRect(nowX,nowY,20,20);
}
}
private void drawline(Graphics g)//画格子
{ for(int i=1;i<11;i++){//横线 x坐标不边,y分别分成10等分,从上画下,
g.drawLine(startX,startY+200/10*i,startX+200,startY+200/10*i);
//竖线
g.drawLine(startX+200/10*i,startY,startX+200/10*i,startY+200);
}
}
//填充
private void fillline(){}
//调整越界坐标
private void ifmaxmingolong()
{
if(nowX<startX){nowX+=20;}
if(nowY<startY){nowY+=20;}
if(nowX>startX+180){nowX-=20;}
if(nowY>startY+180){nowY-=20;}
}
//key
public void keyPressed(int Keycode){//k小写
int action=this.getGameAction(Keycode);
switch(action){
case Canvas.FIRE:forOneTime=1;repaint();break;
case Canvas.DOWN:forOneTime=2;repaint();break;
case Canvas.UP: forOneTime=3;repaint();break;
case Canvas.LEFT:forOneTime=4;repaint();break;
case Canvas.RIGHT:forOneTime=5;repaint();break;
default:break;
}
}
}
评论