读书人

高手快来救命:小弟我做了黑色和红色两

发布时间: 2012-03-13 11:21:12 作者: rapoo

高手快来救命:我做了黑色和红色两个小球,想让它们同时运行,可就是不同时运行。
完整代码如下:


import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import java.util.*;
import javax.microedition.midlet.MIDletStateChangeException;


public class MoveBall extends MIDlet implements CommandListener {
private Display display;
private Command cmdExit;
Ball ball;
RedBall redball;
public MoveBall() {
// TODO 自动生成构造函数存根
display=Display.getDisplay(this);
cmdExit=new Command( "退出 ",Command.SCREEN,1);
ball=new Ball(display);
ball.addCommand(cmdExit);
ball.setCommandListener(this);
redball=new RedBall(display);
redball.addCommand(cmdExit);
redball.setCommandListener(this);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO 自动生成方法存根
ball.destroy();
//notifyDestroyed();
}

protected void pauseApp() {
// TODO 自动生成方法存根

}

protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
ball.start();
redball.start();
}
public void commandAction(Command c,Displayable d)
{
if(c==cmdExit)
{
notifyDestroyed();
}
}

public class Ball extends Canvas implements Runnable {
Random random=new Random();
Display display;
int posX=50;
int posY=50;
int ballsize=30;

public Ball(Display display)
{
super();
this.display=display;
}

/*public void run()
{
while(true)
{
this.posX=(random.nextInt()> > > 1)%(this.getWidth()-20)+10;
this.posY=(random.nextInt()> > > 1)%(this.getHeight()-20)+10;
try{
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
repaint();
}
}*/
public void run()
{

while(true)
{
this.posY += 10;
if(this.posY > this.getHeight()) {
posY = 10;
}

try{
Thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
void start()
{
//posX=(random.nextInt()> > > 1)%(this.getWidth()-20)+10;
//posY=(random.nextInt()> > > 1)%(this.getHeight()-20)+10;
display.setCurrent(this);
Thread t=new Thread(this);
t.start();
repaint();

}
void destroy()
{

}
public void paint(Graphics g)
{
int x=g.getClipX();
int y=g.getClipY();
int width=g.getClipWidth();
int height=g.getClipHeight();
g.setColor(0xffffff);
g.fillRect(x, y, width, height);
g.setColor(30);
g.fillArc(posX, posY, ballsize, ballsize, 0, 360);
}

}

public class RedBall extends Canvas implements Runnable{



int posX=50;
int posY=50;
int ballsize=35;
Display display;




public RedBall(Display display)
{
super();
this.display=display;

}
public void start()
{
display.setCurrent(this);
Thread t=new Thread(this);
t.start();
try{
Thread.sleep(200);
}
catch(Exception e)
{
e.printStackTrace();
}
repaint();
}
public void run()
{
while(true)
{
this.posX+=10;
if(this.posX> this.getWidth())
{
this.posX=30;
}
try{
Thread.sleep(200);
}
catch(Exception ex)
{
ex.printStackTrace();
}
repaint();
}
}
public void paint(Graphics g)
{
int x=g.getClipX();
int y=g.getClipY();
int width=g.getClipWidth();
int height=g.getClipHeight();
g.setColor(0xffffff);
g.fillRect(x, y, width,height);
g.setColor(255, 0, 0);
g.fillArc(posX,posY,ballsize,ballsize,0,360);
}
}
}


[解决办法]
lz的逻辑思路和对象抽象思维不是很清楚啊.
两个ball为什么要继承至canvas?????!!!!!

midlet的display,同一时刻只能显示一个displayable实例.
看lz的代码:
protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
ball.start();
redball.start();
}
你在各自的strat里,显示各自的canvas.
按lz的代码来说,结果只能是显示redball这个canvas实例了.ball不会在显示处理.

应该只有一个canvas对象.lz可以构建两个ball的sprit对象.
多看看例程.

saltedfish


[解决办法]
我做了个笨的,不过你可以借鉴下。有些代码可以优化的。两个球同时运行。

//MainMidlet.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class MainMidlet extends MIDlet {
private static MainMidlet instance;
private Displayable nowScreen;
private Display display;

public MainMidlet() {
instance = this;
nowScreen = new CanvasTest();
}

public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(nowScreen);
}

public void pauseApp() {
nowScreen = display.getCurrent();
}

public void destroyApp(boolean unconditional) {
}

public static void quitApp() {
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}
}
//---------------------------------

//CanvasTest.java
import javax.microedition.lcdui.*;
import java.util.*;

public class CanvasTest extends Canvas implements CommandListener, Runnable {

private class Ball {
public int posX;
public int posY;
public int dx;
public int dy;
public int size;
public int color;
public Ball(int _posX, int _posY, int _dx, int _dy, int _size, int _color) {
posX = _posX;
posY = _posY;
dx = _dx;
dy = _dy;
size = _size;
color = _color;
}
}

private Ball redBall;
private Ball blueBall;


public CanvasTest() {
Random rand = new Random();
int dx = (rand.nextInt() > > > 1) % 5 + 10;
dx = ((rand.nextInt() > > > 1) % 2 == 0) ? (dx * (-1)) : dx;
int dy = (rand.nextInt() > > > 1) % 6 + 2;
dy = ((rand.nextInt() > > > 1) % 2 == 0) ? (dy * (-1)) : dy;
redBall = new Ball(5, 5, dx, dy, 20, 0x00FF0000);
dx = (rand.nextInt() > > > 1) % 5 + 10;
dx = ((rand.nextInt() > > > 1) % 2 == 0) ? (dx * (-1)) : dx;
dy = (rand.nextInt() > > > 1) % 6 + 2;
dy = ((rand.nextInt() > > > 1) % 2 == 0) ? (dy * (-1)) : dy;
blueBall = new Ball(50, 50, dx, dy, 20, 0x000000FF);
addCommand(new Command( "退出 ", Command.EXIT, 1));
setCommandListener(this);
Thread t = new Thread(this);
t.start();
}

public void commandAction(Command _command, Displayable _displayable) {
if (_command.getCommandType() == Command.EXIT) {
MainMidlet.quitApp();
}
}

public void run() {
Random rand = new Random();
int dx = 5;
int dy = (rand.nextInt() > > > 1) % 6 + 2;
while(true) {

redBall.posX += redBall.dx;
redBall.posY += redBall.dy;

blueBall.posX += blueBall.dx;
blueBall.posY += blueBall.dy;

if((redBall.posX + redBall.size > getWidth()) || (redBall.posX < 0)) {
redBall.dx = -redBall.dx;
}

if((redBall.posY + redBall.size > getHeight()) || (redBall.posY < 0)) {
redBall.dy = -redBall.dy;
}

if((blueBall.posX + blueBall.size > getWidth()) || (blueBall.posX < 0)) {
blueBall.dx = -blueBall.dx;
}

if((blueBall.posY + blueBall.size > getHeight()) || (blueBall.posY < 0)) {
blueBall.dy = -blueBall.dy;
}

try {
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
repaint();
}
}

protected void paint(Graphics g) {
//缓冲清屏
g.setColor(0x00FFFFFF);
g.fillRect(0, 0, getWidth(), getHeight());

//开始画
g.setColor(redBall.color);
g.fillArc(redBall.posX, redBall.posY, redBall.size, redBall.size, 0, 360);

g.setColor(blueBall.color);
g.fillArc(blueBall.posX, blueBall.posY, blueBall.size, blueBall.size, 0, 360);
}
}

读书人网 >J2ME开发

热点推荐