 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matheas Manssen Guest
|
Posted: Fri May 12, 2006 5:07 pm Post subject: 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();
}
}
} |
|
| Back to top |
|
 |
Rhino Guest
|
Posted: Sat May 13, 2006 12:07 am Post subject: Re: StarTrek class hangs |
|
|
"Matheas Manssen" <geheim> wrote in message
news:4464b773$0$2029$ba620dc5 (AT) text (DOT) nova.planet.nl...
| Quote: | 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
| Quote: |
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();
}
}
}
|
|
|
| Back to top |
|
 |
Matheas Manssen Guest
|
Posted: Sat May 13, 2006 8:07 pm Post subject: Re: StarTrek class hangs |
|
|
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 (AT) nospam (DOT) com> schreef in bericht
news:i099g.7951$aq5.382895 (AT) news20 (DOT) bellglobal.com...
| Quote: |
"Matheas Manssen" <geheim> wrote in message
news:4464b773$0$2029$ba620dc5 (AT) text (DOT) 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
|
|
|
| Back to top |
|
 |
Paul Hamaker Guest
|
Posted: Sat May 13, 2006 10:07 pm Post subject: Re: StarTrek class hangs |
|
|
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 |
|
| Back to top |
|
 |
Rhino Guest
|
Posted: Sat May 13, 2006 11:07 pm Post subject: Re: StarTrek class hangs |
|
|
"Matheas Manssen" <geheim> wrote in message
news:44663650$0$2019$ba620dc5 (AT) text (DOT) nova.planet.nl...
| Quote: | 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.
| Quote: | 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 |
|
| Back to top |
|
 |
Darryl L. Pierce Guest
|
Posted: Mon May 15, 2006 4:07 pm Post subject: Re: StarTrek class hangs |
|
|
Matheas Manssen wrote:
| Quote: | 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 (AT) gmail (DOT) 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 *** |
|
| Back to top |
|
 |
Matheas Manssen Guest
|
Posted: Mon May 15, 2006 4:07 pm Post subject: Re: StarTrek class hangs |
|
|
Hi Rhino,
| Quote: | 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.
| Quote: |
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 |
|
| Back to top |
|
 |
Darryl L. Pierce Guest
|
Posted: Mon May 15, 2006 4:07 pm Post subject: Re: StarTrek class hangs |
|
|
Matheas Manssen wrote:
| Quote: | 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 (AT) gmail (DOT) 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 *** |
|
| Back to top |
|
 |
Matheas Manssen Guest
|
Posted: Mon May 15, 2006 4:07 pm Post subject: Re: StarTrek class hangs |
|
|
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 (AT) hotmail (DOT) com> schreef in bericht
news:44665212$0$19910$afc38c87@...
| Quote: | 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 (AT) text (DOT) 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.
By the way, the bug is not in my program, but in the flushGraphics()
method.
Best regards,
Matheas
"Rhino" <no.offline.contact.please (AT) nospam (DOT) com> schreef in bericht
news:i099g.7951$aq5.382895 (AT) news20 (DOT) bellglobal.com...
"Matheas Manssen" <geheim> wrote in message
news:4464b773$0$2029$ba620dc5 (AT) text (DOT) 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
|
|
|
| Back to top |
|
 |
Matheas Manssen Guest
|
Posted: Sat May 20, 2006 6:07 pm Post subject: Re: StarTrek class hangs |
|
|
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 (AT) gmail (DOT) com> schreef in bericht
news:44689343$0$29254$88260bb3 (AT) free (DOT) teranews.com...
| Quote: | 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 (AT) gmail (DOT) 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 *** |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|