Home > Archive > Java Help > May 2006 > StarTrek class hangs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
StarTrek class hangs
|
|
| Matheas Manssen 2006-05-12, 7:06 pm |
| 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();
}
}
}
| |
|
|
"Matheas Manssen" <geheim> wrote in message
news:4464b773$0$2029$ba620dc5@text.nova.planet.nl...
> 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?
>
We'd have a lot better chance of helping you if you gave us the stacktrace
that occurred when you had your problem. That would help us - and you! -
determine exactly where the problem occurred and why.
We're not mindreaders and it's probably unrealistic to assume that we're
just going to paste the program into our IDEs to debug it for you.
Rhino
>
> 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();
> }
> }
> }
>
| |
| Matheas Manssen 2006-05-13, 7:03 pm |
| Hi Rhine,
Thank you for your reply.
I understand it's too much asked for you to put the program in your IDE.
However, I don't know how to generate a stack trace.
By the way, the bug is not in my program, but in the flushGraphics() method.
Best regards,
Matheas
"Rhino" <no.offline.contact.please@nospam.com> schreef in bericht
news:i099g.7951$aq5.382895@news20.bellglobal.com...
>
> "Matheas Manssen" <geheim> wrote in message
> news:4464b773$0$2029$ba620dc5@text.nova.planet.nl...
> We'd have a lot better chance of helping you if you gave us the stacktrace
> that occurred when you had your problem. That would help us - and you! -
> determine exactly where the problem occurred and why.
>
> We're not mindreaders and it's probably unrealistic to assume that we're
> just going to paste the program into our IDEs to debug it for you.
>
> Rhino
>
| |
| Paul Hamaker 2006-05-13, 7:03 pm |
| Runs fine on my J2ME Wireless Toolkit, Java 1.5.0_06, WinXP Home. Let
it run for a while, no errors or quirks.
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
| |
|
|
"Matheas Manssen" <geheim> wrote in message
news:44663650$0$2019$ba620dc5@text.nova.planet.nl...
> Hi Rhine,
>
> Thank you for your reply.
>
> I understand it's too much asked for you to put the program in your IDE.
> However, I don't know how to generate a stack trace.
You said the program was hanging. Usually, a stack trace is the first
symptom of a hang and appears more or less automatically.
When you say "hang" do you mean that the program just stops in the middle
without a stack trace? The word "hang" is sometimes used quite loosely. I
just assumed that you meant you were getting a stack trace, which will be
produced when you throw an Exception.
> By the way, the bug is not in my program, but in the flushGraphics()
> method.
>
Isn't the flushGraphics() method IN your program? Even if flushGraphics() is
an existing method from the Java API that you are overriding, the overriding
code is generally considered be in your program. If you did something
inappropriate in that code, the program could easily hang. Or are you saying
that your program invokes a flushGraphics() method from the API without
overriding it?
And how do you know that the bug is in flushGraphics() if you're not seeing
a stack trace?
--
Rhino
| |
| Darryl L. Pierce 2006-05-15, 7:10 pm |
| Matheas Manssen wrote:
> class MyGameCanvas extends GameCanvas {
> private Graphics graphics;
You can't do this. The graphics object is only valid during the life of
the call to Canvas.paint(). After that, the object is not usable.
--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
*** Posted via a free Usenet account from http://www.teranews.com ***
| |
| Darryl L. Pierce 2006-05-15, 7:10 pm |
| Matheas Manssen wrote:
> public void commandAction(Command command, Displayable screen) {
> if (command == exitCommand) {
> destroyApp(false);
> notifyDestroyed();
> }
> }
> }
This code here is not proper; i.e., destroyApp() is a callback for the
platform to notify the MIDlet it's being destroyed. You should not be
calling it yourself.
--
Darryl L. Pierce <mcpierce@gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
*** Posted via a free Usenet account from http://www.teranews.com ***
| |
| Matheas Manssen 2006-05-15, 7:10 pm |
| Hi,
The screen is no longer updated. I used debug statements(System.out.println)
to determine were the problem is. It happens to be in the flushGraphics
method. The added EXIT command still works.
Best regards,
Matheas
"Ian Michael Gumby" <im_gumbyNoSpam@hotmail.com> schreef in bericht
news:44665212$0$19910$afc38c87@...
>I think what he's asking is if you get a stack trace from an exception
>being thrown.
>
> Since it "hangs" I am going to assume you mean that the program doesn't
> display anything?
> And thus there is no output?
> -G
>
> "Matheas Manssen" <geheim> wrote in message
> news:44663650$0$2019$ba620dc5@text.nova.planet.nl...
>
>
| |
| Matheas Manssen 2006-05-15, 7:10 pm |
| Hi Rhino,
> You said the program was hanging. Usually, a stack trace is the first
> symptom of a hang and appears more or less automatically.
>
Sorry, what I meant was that the screen is no longer updated. The EXIT
command which I added still works.
>
> Isn't the flushGraphics() method IN your program? Even if flushGraphics()
> is an existing method from the Java API that you are overriding, the
> overriding code is generally considered be in your program. If you did
> something inappropriate in that code, the program could easily hang. Or
> are you saying that your program invokes a flushGraphics() method from the
> API without overriding it?
Indeed, I haven't overriden it.
>
> And how do you know that the bug is in flushGraphics() if you're not
> seeing a stack trace?
I used debug statements(System.out.println).
Best regards,
Matheas
| |
| Matheas Manssen 2006-05-20, 7:04 pm |
| Hi Darryl,
It's the Graphics object of a GameCanvas. You can use it during the life of
a the GameCanvas. So, it differs from the Graphics object which you get
passed in the paint() method.
Cheers,
Matheas Manssen
"Darryl L. Pierce" <mcpierce@gmail.com> schreef in bericht
news:44689343$0$29254$88260bb3@free.teranews.com...
> Matheas Manssen wrote:
>
> You can't do this. The graphics object is only valid during the life of
> the call to Canvas.paint(). After that, the object is not usable.
>
>
> --
> Darryl L. Pierce <mcpierce@gmail.com>
> http://mcpierce.multiply.com/
> "What do you care what people think, Mr. Feynman?"
>
> *** Posted via a free Usenet account from http://www.teranews.com ***
|
|
|
|
|