请高人给J2ME代码注释一下
代码如下,请给主要语句注释,非常感谢importjavax.microedition.lcdui.Display;importjavax.microedition.lcd...
代码如下,请给主要语句注释,非常感谢
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class pic extends MIDlet
{
private Display display;
ImageCanvas ic;
public pic()
{
display=Display.getDisplay(this);
ic=new ImageCanvas();
display.setCurrent(ic);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
protected void pauseApp()
{
}
protected void startApp() throws MIDletStateChangeException
{
ic.start();
display.setCurrent(ic);
}
}
class ImageCanvas extends GameCanvas implements Runnable
{
private Image img01;
private Image img02;
private Sprite sp1;
private Sprite sp2;
private Graphics gra;
private Graphics g;
public ImageCanvas()
{
super(true);
try
{
img01 = Image.createImage("/002.png");
img02 = Image.createImage("/001.png");
sp1 = new Sprite(img01);
sp2 = new Sprite(img02);
gra = this.getGraphics();
sp1.setPosition(0, 0);
sp2.setPosition(200, 210);
sp1.paint(gra);
sp2.paint(gra);
new Thread(this).start();
}catch(Exception e)
{
e.printStackTrace();
}
}
public void start()
{
}
public void run()
{
int sp1_p=1;
int sp2_p=-1;
while(true)
{
gra.setColor(255,255,255);
gra.fillRect(0, 0, getWidth(), getHeight());//
sp1.move(sp1_p , sp1_p);
sp2.move(sp2_p, sp2_p);
sp1.paint(gra);
sp2.paint(gra);
this.flushGraphics();
if(sp1.collidesWith(sp2,true))
{
sp1_p=-1;
sp2_p=1;
}
if(sp2.collidesWith(sp1,true))
{
}
if(sp1.collidesWith(sp2,true))
{
}
if(sp1.getX()==0)
{
sp1_p=1;
}
if(sp2.getX()==this.getWidth()-sp2.getWidth())
{
sp2_p=-1;
}
if(sp1.getY()==this.getWidth()-sp2.getWidth())
{
}
try
{
Thread.currentThread();
Thread.sleep(50);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
} 展开
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class pic extends MIDlet
{
private Display display;
ImageCanvas ic;
public pic()
{
display=Display.getDisplay(this);
ic=new ImageCanvas();
display.setCurrent(ic);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
protected void pauseApp()
{
}
protected void startApp() throws MIDletStateChangeException
{
ic.start();
display.setCurrent(ic);
}
}
class ImageCanvas extends GameCanvas implements Runnable
{
private Image img01;
private Image img02;
private Sprite sp1;
private Sprite sp2;
private Graphics gra;
private Graphics g;
public ImageCanvas()
{
super(true);
try
{
img01 = Image.createImage("/002.png");
img02 = Image.createImage("/001.png");
sp1 = new Sprite(img01);
sp2 = new Sprite(img02);
gra = this.getGraphics();
sp1.setPosition(0, 0);
sp2.setPosition(200, 210);
sp1.paint(gra);
sp2.paint(gra);
new Thread(this).start();
}catch(Exception e)
{
e.printStackTrace();
}
}
public void start()
{
}
public void run()
{
int sp1_p=1;
int sp2_p=-1;
while(true)
{
gra.setColor(255,255,255);
gra.fillRect(0, 0, getWidth(), getHeight());//
sp1.move(sp1_p , sp1_p);
sp2.move(sp2_p, sp2_p);
sp1.paint(gra);
sp2.paint(gra);
this.flushGraphics();
if(sp1.collidesWith(sp2,true))
{
sp1_p=-1;
sp2_p=1;
}
if(sp2.collidesWith(sp1,true))
{
}
if(sp1.collidesWith(sp2,true))
{
}
if(sp1.getX()==0)
{
sp1_p=1;
}
if(sp2.getX()==this.getWidth()-sp2.getWidth())
{
sp2_p=-1;
}
if(sp1.getY()==this.getWidth()-sp2.getWidth())
{
}
try
{
Thread.currentThread();
Thread.sleep(50);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
} 展开
1个回答
展开全部
public class pic extends MIDlet
{
private Display display;
ImageCanvas ic;
public pic()
{
//首先display 是屏幕的对象,用Display.getDisplay(this); 的方法得到;
display=Display.getDisplay(this);
ic=new ImageCanvas();
//display.setCurrent(ic);是表示设置的当前的屏幕画布是ic(这个对象)
display.setCurrent(ic);
}
//是销毁midelt对象时时调用
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
protected void pauseApp()
{
}
//startApp()启动
protected void startApp() throws MIDletStateChangeException
{
ic.start();
display.setCurrent(ic);
}
}
/**3,class ImageCanvas extends GameCanvas implements Runnable 这是个内部类继承GameCanvas 游戏画布类,以及Runnable 多线程
*/
class ImageCanvas extends GameCanvas implements Runnable
{
private Image img01;
private Image img02;
private Sprite sp1; //游戏精灵类 比如坦克大战中的坦克
private Sprite sp2;
private Graphics gra;
private Graphics g;
public ImageCanvas()
{
super(true);
try
{
img01 = Image.createImage("/002.png");
img02 = Image.createImage("/001.png");
sp1 = new Sprite(img01);
sp2 = new Sprite(img02);
gra = this.getGraphics();//得到画笔
sp1.setPosition(0, 0); //设置游戏精灵出现的位置
sp2.setPosition(200, 210);
sp1.paint(gra); //将精灵画在界面上
sp2.paint(gra);
new Thread(this).start(); //启动线程调用run()方法
}catch(Exception e)
{
e.printStackTrace();
}
}
public void start()
{
}
public void run()
{
int sp1_p=1;
int sp2_p=-1;
while(true)
{
gra.setColor(255,255,255);//设置背景为白色
gra.fillRect(0, 0, getWidth(), getHeight());// 画个包含整个界面矩形
//上面两个方法作用是清空屏幕
sp1.move(sp1_p , sp1_p); //移动x方法向右,y方向向左
sp2.move(sp2_p, sp2_p);
sp1.paint(gra); //画出精灵
sp2.paint(gra);
this.flushGraphics(); //输出缓冲
//判断两个精灵是否碰撞 碰撞了返回true
if(sp1.collidesWith(sp2,true))
{
sp1_p=-1;
sp2_p=1;
}
if(sp2.collidesWith(sp1,true))
{
}
if(sp1.collidesWith(sp2,true))
{
}
if(sp1.getX()==0)
{
sp1_p=1;
}
if(sp2.getX()==this.getWidth()-sp2.getWidth())
{
sp2_p=-1;
}
if(sp1.getY()==this.getWidth()-sp2.getWidth())
{
}
try
{
Thread.currentThread();
Thread.sleep(50); //线程休眠50毫秒
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
建议你去看看网络上的视频教学 http://www.enet.com.cn/eschool/video/j2me/
{
private Display display;
ImageCanvas ic;
public pic()
{
//首先display 是屏幕的对象,用Display.getDisplay(this); 的方法得到;
display=Display.getDisplay(this);
ic=new ImageCanvas();
//display.setCurrent(ic);是表示设置的当前的屏幕画布是ic(这个对象)
display.setCurrent(ic);
}
//是销毁midelt对象时时调用
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
}
protected void pauseApp()
{
}
//startApp()启动
protected void startApp() throws MIDletStateChangeException
{
ic.start();
display.setCurrent(ic);
}
}
/**3,class ImageCanvas extends GameCanvas implements Runnable 这是个内部类继承GameCanvas 游戏画布类,以及Runnable 多线程
*/
class ImageCanvas extends GameCanvas implements Runnable
{
private Image img01;
private Image img02;
private Sprite sp1; //游戏精灵类 比如坦克大战中的坦克
private Sprite sp2;
private Graphics gra;
private Graphics g;
public ImageCanvas()
{
super(true);
try
{
img01 = Image.createImage("/002.png");
img02 = Image.createImage("/001.png");
sp1 = new Sprite(img01);
sp2 = new Sprite(img02);
gra = this.getGraphics();//得到画笔
sp1.setPosition(0, 0); //设置游戏精灵出现的位置
sp2.setPosition(200, 210);
sp1.paint(gra); //将精灵画在界面上
sp2.paint(gra);
new Thread(this).start(); //启动线程调用run()方法
}catch(Exception e)
{
e.printStackTrace();
}
}
public void start()
{
}
public void run()
{
int sp1_p=1;
int sp2_p=-1;
while(true)
{
gra.setColor(255,255,255);//设置背景为白色
gra.fillRect(0, 0, getWidth(), getHeight());// 画个包含整个界面矩形
//上面两个方法作用是清空屏幕
sp1.move(sp1_p , sp1_p); //移动x方法向右,y方向向左
sp2.move(sp2_p, sp2_p);
sp1.paint(gra); //画出精灵
sp2.paint(gra);
this.flushGraphics(); //输出缓冲
//判断两个精灵是否碰撞 碰撞了返回true
if(sp1.collidesWith(sp2,true))
{
sp1_p=-1;
sp2_p=1;
}
if(sp2.collidesWith(sp1,true))
{
}
if(sp1.collidesWith(sp2,true))
{
}
if(sp1.getX()==0)
{
sp1_p=1;
}
if(sp2.getX()==this.getWidth()-sp2.getWidth())
{
sp2_p=-1;
}
if(sp1.getY()==this.getWidth()-sp2.getWidth())
{
}
try
{
Thread.currentThread();
Thread.sleep(50); //线程休眠50毫秒
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
建议你去看看网络上的视频教学 http://www.enet.com.cn/eschool/video/j2me/
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询