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.microedit ion.midlet.*;
import javax.microedit ion.lcdui.*;
import javax.microedit ion.lcdui.game. *;
import java.util.*;
import java.lang.*;
//Bestand: MyGameCanvas.ja va
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 surpressKeyEven ts ) {
super( surpressKeyEven ts );
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.setCol or( color );
graphics.fillRe ct( 0, 0, width, height );
color = 0x00FFFFFF;
graphics.setCol or( 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 coordiantenstel sel van scherm
px = px + width/2;
py = py + height/2;
// Zet ster op scherm
color = 0x00FFFFFF;
graphics.setCol or( color );
graphics.drawLi ne( 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.addC ommand(exitComm and);
gameCanvas.setC ommandListener( this);
// Set the MIDlet's display to its initial screen
display = Display.getDisp lay(this);
display.setCurr ent(gameCanvas) ;
timerTask = new MyTimerTask();
timerTask.setCa nvas( gameCanvas );
timer = new Timer();
timer.schedule( timerTask, 1000, 30);
}
}
public void pauseApp() {
}
public void destroyApp(bool ean unconditional) {
}
public void commandAction(C ommand command, Displayable screen) {
if (command == exitCommand) {
destroyApp(fals e);
notifyDestroyed ();
}
}
}
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.microedit ion.midlet.*;
import javax.microedit ion.lcdui.*;
import javax.microedit ion.lcdui.game. *;
import java.util.*;
import java.lang.*;
//Bestand: MyGameCanvas.ja va
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 surpressKeyEven ts ) {
super( surpressKeyEven ts );
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.setCol or( color );
graphics.fillRe ct( 0, 0, width, height );
color = 0x00FFFFFF;
graphics.setCol or( 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 coordiantenstel sel van scherm
px = px + width/2;
py = py + height/2;
// Zet ster op scherm
color = 0x00FFFFFF;
graphics.setCol or( color );
graphics.drawLi ne( 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.addC ommand(exitComm and);
gameCanvas.setC ommandListener( this);
// Set the MIDlet's display to its initial screen
display = Display.getDisp lay(this);
display.setCurr ent(gameCanvas) ;
timerTask = new MyTimerTask();
timerTask.setCa nvas( gameCanvas );
timer = new Timer();
timer.schedule( timerTask, 1000, 30);
}
}
public void pauseApp() {
}
public void destroyApp(bool ean unconditional) {
}
public void commandAction(C ommand command, Displayable screen) {
if (command == exitCommand) {
destroyApp(fals e);
notifyDestroyed ();
}
}
}