File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Images don't show up with first click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Images don Watch "Images don New topic
Author

Images don't show up with first click

Dustin Yonak
Greenhorn

Joined: Jun 14, 2008
Posts: 6
Ok so I have a problem with the code below, mostly everything works as I would expect but the stoplight picture doesn't show up until I click one of the buttons (red, yellow, or green) twice. The really odd part is the step variable gets updated which is in the same code block that initiates the drawing of the light. Once any button has been pressed twice that image shows properly and promptly after any other button presses. Anotherwards the app works exactly as it should once I press each of the buttons twice. It's my first posting here so I apologize if this is the wrong forum or improperly formatted.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

That whole thing you're doing, where you call getGraphics() in an event handler and do your painting there -- that just isn't how it's done. Not only will strange things happen (as you've observed) the application won't update properly when you resize, cover, minimize or move the window.

The proper thing to do is to set flags and other state variables in your event handlers, override the paintComponent() method of a Swing component, and use the Graphics passed as an argument to that mehtod to do all your painting.


[Jess in Action][AskingGoodQuestions]
Dustin Yonak
Greenhorn

Joined: Jun 14, 2008
Posts: 6
First off, thank you for the very quick reply!

Ok, I think I understand. You're saying I should not call getGraphics() in the event but just setup my variables there and then pass them to methods to perform the draw. I agree with this and understand it's a better approach. I've altered my code to the following but I'm still getting the lag where I have to click the button twice to get the graphic to show up so it may be unrelated.

Here's the new code:



I've removed the timer that facilitates the animation of the car while the light is yellow/green out to simplify/troubleshoot but still no luck. If anyone sees any problems here please let me know, I really want to learn how to do this the right way.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Naw, you're still doing the same thing. Check out this tutorial. By the time they get to the program named SwingPaintDemo3, they're doing exactly what I'm recommending: set variables in your event handlers and call repaint(); then override paintComponent(), and use the variables to do the painting in there.
Dustin Yonak
Greenhorn

Joined: Jun 14, 2008
Posts: 6
Thanks again Ernest, I'm currently working through the tutorial you provided but I just wanted to let you know I appreciate your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Images don't show up with first click