Win a copy of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home this week in the General Computing forum!
Forums Login/signup

Problems with my run() method.

+Pie Number of slices to send: Send
basically, it doesnt do anything, it runs my paint() method once when it starts up, and then just sits there.
+Pie Number of slices to send: Send
What is the issue you are facing?
1
+Pie Number of slices to send: Send
Why on earth would you want to repaint something every 20 ms anyway? And I strongly suspect that you're running that while(true) and Thread.sleep(...) on the EDT -- both very big No-Nos.

It rather looks like you need to learn and understand event driven programming and/or the use of javax.swing.Timer.

To get better help sooner, post a SSCCE that clearly demonstrates your problem.
+Pie Number of slices to send: Send
I think he expects the programm to repaint every 20ms, but he only sees it painting once.
From what i've learned, even if you call repaint() every 20ms, it wont actually do any painting unless
something needs to be repainted. If everything still is painted correctly, as it was 20ms ago, it wont repaint.
+Pie Number of slices to send: Send
And even if there is something to repaint(), it often takes much longer than 20ms, so that will interfere with the next call.
1
+Pie Number of slices to send: Send
repaint() will always queue a PaintEvent.

PaintEvents that haven't been processed will be coalesced.

If the EDT is tied up in a while(true) loop and/or sleep(...)ing on the job, those PaintEvents will be coalesced forever.
+Pie Number of slices to send: Send
Alright people, I really feel stupid now. I didnt mean to type 20 in the code, and when it didnt work I just sort of dismissed that segment of code. thanks for the replies, and sorry for the stupid post.
+Pie Number of slices to send: Send
nope, its still not working, would it help if i mentioned that this is for an applet game?
+Pie Number of slices to send: Send
after further the detective research, the run() method is not being called at all.

please forgive my incredible sloppiness, I'm new to this.
+Pie Number of slices to send: Send
 

Wes McClintock wrote:. . . sorry for the stupid post.

What stupid post?
+Pie Number of slices to send: Send
 

Darryl Burke wrote:Why on earth would you want to repaint something every 20 ms anyway? And I strongly suspect that you're running that while(true) and Thread.sleep(...) on the EDT -- both very big No-Nos.

It rather looks like you need to learn and understand event driven programming and/or the use of javax.swing.Timer.

To get better help sooner, post a SSCCE that clearly demonstrates your problem.



yes, your probably right that im running it on the edt, I dont really know the mechanics of what im doing, but ive only been programming for a few months so im not surprised. Strangely though, almost every applet game ive seen uses this code.
+Pie Number of slices to send: Send
 

Wes McClintock wrote:Strangely though, almost every applet game ive seen uses this code.



Do they all use Applet instead of JApplet? JApplet replaced Applet in 1998 when Swing and Java 2 came out; applet writers are late adopters because their users are likely to be using old JVMs, but it's been 13 years and you'd think that Applet would now be well and truly extinct.

It's possible that single-threaded code like that worked with Applet, but JApplet is a bit more complex and threading issues do matter.
+Pie Number of slices to send: Send
 

Paul Clapham wrote:

Wes McClintock wrote:Strangely though, almost every applet game ive seen uses this code.



Do they all use Applet instead of JApplet? JApplet replaced Applet in 1998 when Swing and Java 2 came out; applet writers are late adopters because their users are likely to be using old JVMs, but it's been 13 years and you'd think that Applet would now be well and truly extinct.

It's possible that single-threaded code like that worked with Applet, but JApplet is a bit more complex and threading issues do matter.


really? well, I'm out of date and I just started. But ill definitely look into JApplet if its that old. im kinda surprised ive never heard of it actually.
+Pie Number of slices to send: Send
The thing about finding information on the Internet is, you're more likely to find information the more links there are to that information. And information which has been out there longer is going to have more links... and therefore other people are more likely to find it and link to it. So there's a bias against newer versions of things.
+Pie Number of slices to send: Send
1) In your overridden paint method you should call super.paint(g); as the first statement.
2) You shouldn't override update like that. Repainting and updating are two different operations. Just drop the entire method.
3) You should use the result of getGraphics() (offgra) but the argument provided to the paint method (g) instead. Drop offgra completely.
+Pie Number of slices to send: Send
Did you miss out a "not" in no 3, Rob?

I think this discussion would sit better on our GUIs forum, so shall move it.
2
+Pie Number of slices to send: Send
 

Wes McClintock wrote:after further the detective research, the run() method is not being called at all.


It wouldn't. Because you are not passing the runnable to the Thread constructor at all.
Your code should be written as:

and its poor coding convention to use a single uppercase letter for an instance name...change the 'T' to 't'.
+Pie Number of slices to send: Send
 

Rob Spoor wrote:1) In your overridden paint method you should call super.paint(g); as the first statement.
2) You shouldn't override update like that. Repainting and updating are two different operations. Just drop the entire method.
3) You should use the result of getGraphics() (offgra) but the argument provided to the paint method (g) instead. Drop offgra completely.


I'm not sure what you mean in # 3, but I've done it that way in the past 2 applet games I wrote, so i think ill just stick with it for a while. (I know that sounds stupid, but I'm making this game to practice objects etc).


reply
reply
This thread has been viewed 1541 times.
Similar Threads
target Runnable of a thread
Get report from running external tests
Call Overload Version of Thread's run method
A quick question on threads
Finalize():will garbage collector call finalize() after my code has already called it?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 27, 2025 08:47:41.