aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Problems with my run() method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problems with my run() method." Watch "Problems with my run() method." New topic
Author

Problems with my run() method.

Wes McClintock
Greenhorn

Joined: Jul 19, 2011
Posts: 25
basically, it doesnt do anything, it runs my paint() method once when it starts up, and then just sits there.
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
What is the issue you are facing?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

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.

luck, db
There are no new questions, but there may be new answers.
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

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.


Knowledge Reigns Supreme
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: May 03, 2008
Posts: 4658
    
    5

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

Joined: Jul 19, 2011
Posts: 25
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

Joined: Jul 19, 2011
Posts: 25
nope, its still not working, would it help if i mentioned that this is for an applet game?
Wes McClintock
Greenhorn

Joined: Jul 19, 2011
Posts: 25
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

Joined: Oct 13, 2005
Posts: 39828
    
  28
Wes McClintock wrote:. . . sorry for the stupid post.
What stupid post?
Wes McClintock
Greenhorn

Joined: Jul 19, 2011
Posts: 25
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
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

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

Joined: Jul 19, 2011
Posts: 25
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
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: Oct 16, 2003
Posts: 1089
    
  10

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


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Wes McClintock
Greenhorn

Joined: Jul 19, 2011
Posts: 25
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).

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with my run() method.