JAVA实现,在屏幕内一个小球随机移动
实现小球在屏幕内自由运动,到达边界后能反弹,要求两个类,一个MIDlet,一个普通类。(J2ME)...
实现小球在屏幕内自由运动,到达边界后能反弹,要求两个类,一个MIDlet,一个普通类。(J2ME)
展开
2013-07-20
展开全部
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;import com.sun.midp.lcdui.DisplayDeviceAccess;
public class Main extends MIDlet {
public static Display display;
public static GameCanvas Game;
public static MIDlet mid;
public Main() {
Game=new GameCanvas();
display=Display.getDisplay(this);
mid=this;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {display.setCurrent(Game);}
}
Canvas类=========================import java.util.Random;import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class GameCanvas extends Canvas implements Runnable{
private int BallX=0;
private int BallY=0;
private int BallW=20;
private int BallH=20;
private boolean BallRun=true;
private byte BallDir=0;
private final byte Left=1;
private final byte Right=2;
private final int ScreenWith=150;
private final int ScreenHight=200;
private final int Speed = 5;
Random rand;
public GameCanvas(){
rand=new Random();
int state=rand.nextInt()%2;
BallDir=(byte)(state==0?Left:Right);
BallX=rand.nextInt()%240;
BallY=rand.nextInt()%320;
new Thread(this).start();
this.setFullScreenMode(true);
}
protected void paint(Graphics g) {
g.setColor(0);
g.fillRect(0, 0,this.getWidth(), this.getHeight());
g.setColor(255, 255,255);
g.fillArc(BallX, BallY,BallW, BallH, 0,360);
} public void BallRun(){
switch(BallDir){
case Left:
if(BallX>0){BallX-=Speed;}
else{BallDir=Right;};break;
case Right:
if(BallX<ScreenWith){BallX+=Speed;}
else{BallDir=Left;}break;
}
if(BallY>=0&&BallRun){
BallY-=Speed;
}else{BallRun=false;}
if(BallY<=ScreenHight&&!BallRun){
BallY+=Speed;
}else{BallRun=true;}
}
public void run() {
while(true){
BallRun();
this.repaint();
try{Thread.sleep(100);<br> }catch(Exception ex){ex.printStackTrace();}
} }}
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;import com.sun.midp.lcdui.DisplayDeviceAccess;
public class Main extends MIDlet {
public static Display display;
public static GameCanvas Game;
public static MIDlet mid;
public Main() {
Game=new GameCanvas();
display=Display.getDisplay(this);
mid=this;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {display.setCurrent(Game);}
}
Canvas类=========================import java.util.Random;import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class GameCanvas extends Canvas implements Runnable{
private int BallX=0;
private int BallY=0;
private int BallW=20;
private int BallH=20;
private boolean BallRun=true;
private byte BallDir=0;
private final byte Left=1;
private final byte Right=2;
private final int ScreenWith=150;
private final int ScreenHight=200;
private final int Speed = 5;
Random rand;
public GameCanvas(){
rand=new Random();
int state=rand.nextInt()%2;
BallDir=(byte)(state==0?Left:Right);
BallX=rand.nextInt()%240;
BallY=rand.nextInt()%320;
new Thread(this).start();
this.setFullScreenMode(true);
}
protected void paint(Graphics g) {
g.setColor(0);
g.fillRect(0, 0,this.getWidth(), this.getHeight());
g.setColor(255, 255,255);
g.fillArc(BallX, BallY,BallW, BallH, 0,360);
} public void BallRun(){
switch(BallDir){
case Left:
if(BallX>0){BallX-=Speed;}
else{BallDir=Right;};break;
case Right:
if(BallX<ScreenWith){BallX+=Speed;}
else{BallDir=Left;}break;
}
if(BallY>=0&&BallRun){
BallY-=Speed;
}else{BallRun=false;}
if(BallY<=ScreenHight&&!BallRun){
BallY+=Speed;
}else{BallRun=true;}
}
public void run() {
while(true){
BallRun();
this.repaint();
try{Thread.sleep(100);<br> }catch(Exception ex){ex.printStackTrace();}
} }}
2013-07-20
展开全部
你只需要一个随机数、 然后 画一个球形 让它随随机数动就是了。用一个线程控制 时间 就可以了 我这里没有MIDlet。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询