• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with my run() method.

 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically, it doesnt do anything, it runs my paint() method once when it starts up, and then just sits there.
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the issue you are facing?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jon Avadis
Ranch Hand
Posts: 49
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And even if there is something to repaint(), it often takes much longer than 20ms, so that will interfere with the next call.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope, its still not working, would it help if i mentioned that this is for an applet game?
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after further the detective research, the run() method is not being called at all.

please forgive my incredible sloppiness, I'm new to this.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wes McClintock wrote:. . . sorry for the stupid post.
What stupid post?
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
Wes McClintock
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic