StarTrek class hangs
Hi,
When run the following class with midp 2.0, the midlet hangs. I think it
happens in the flushGraphics() method. Does anybody know the solution?
Best regards,
Matheas Manssen
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.lang.*;
//Bestand: MyGameCanvas.java
class MyTimerTask extends TimerTask {
private MyGameCanvas canvas;
public void MyTimerTask( ) {
}
public void setCanvas ( MyGameCanvas myGameCanvas ) {
canvas = myGameCanvas;
}
public void run() {
canvas.update();
}
}
class MyGameCanvas extends GameCanvas {
private static int FOCUSAFSTAND = 50;
private static int STARS = 100;
private Graphics graphics;
private Random random;
int width, height;
int x, y;
int range;
int color;
int starField[][] = new int [STARS][3];
public MyGameCanvas( boolean surpressKeyEvents ) {
super( surpressKeyEvents );
graphics = getGraphics();
random = new Random();
width = getWidth();
height = getHeight();
range = Math.max( width, height );
// vul starField
for ( int star=0; star< STARS; star++ ) {
int x, y, z;
x = random.nextInt() % ( range/2 );
y = random.nextInt() % ( range/2 );
z = Math.abs(random.nextInt()) % ( range );
starField[star][0] = x;
starField[star][1] = y;
starField[star][2] = z;
}
}
public void update() {
color = 0x00000000;
graphics.setColor( color );
graphics.fillRect( 0, 0, width, height );
color = 0x00FFFFFF;
graphics.setColor( color );
// Druk starField af
for ( int star=0; star< STARS; star++ ) {
int x, y, z;
int px, py;
x = starField[star][0];
y = starField[star][1];
z = starField[star][2];
px = ( FOCUSAFSTAND * x ) / (FOCUSAFSTAND + z );
py = ( FOCUSAFSTAND * y ) / (FOCUSAFSTAND + z );
// Omrekenen naar coordiantenstelsel van scherm
px = px + width/2;
py = py + height/2;
// Zet ster op scherm
color = 0x00FFFFFF;
graphics.setColor( color );
graphics.drawLine( px, py, px, py );
// Laat ster een stapje dichterbij komen
starField[star][2] = ( z-1 );
if ( starField[star][2] == 0 ) {
starField[star][2] = range;
}
}
flushGraphics();
}
}
public class StarTrek extends MIDlet implements CommandListener {
private Command exitCommand;
private TextBox textBox;
private Display display;
private MyGameCanvas gameCanvas;
private MyTimerTask timerTask;
private Timer timer;
private boolean firstTime = true;
public void startApp() {
if ( firstTime ) {
firstTime = false;
// Create the abstract command
gameCanvas = new MyGameCanvas( true );
exitCommand = new Command("Exit", Command.EXIT, 1);
gameCanvas.addCommand(exitCommand);
gameCanvas.setCommandListener(this);
// Set the MIDlet's display to its initial screen
display = Display.getDisplay(this);
display.setCurrent(gameCanvas);
timerTask = new MyTimerTask();
timerTask.setCanvas( gameCanvas );
timer = new Timer();
timer.schedule( timerTask, 1000, 30);
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command command, Displayable screen) {
if (command == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
}