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.
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.
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.
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.
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 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.
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.
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.
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).