wood burning stoves 2.0*
The moose likes Applets and the fly likes Loading images in Applet inherited class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Loading images in Applet inherited class" Watch "Loading images in Applet inherited class" New topic
Author

Loading images in Applet inherited class

kourosh parsa
Ranch Hand

Joined: Jan 22, 2012
Posts: 35
    
    1
Hi fellows.
I'm observing a very strange problem.
I have:
- a DrawingBoard class that implements JPanel
- an abstract Element class that implements a JPanel that is added to the DrawingBoard
- classes which implement the Element class for example Res class

Each element should have an image. When I load the image in the constructor of "Res", it does not get drawn until another element is added (next paint event).
I stepped through it; the paintComponent event does fire but the image size upon the first paint is -1 (but the image is NOT null).
Then I tried loading the images in the abstract Element class, and the problem does not occur except for the first time. so I still need to fix it because the first time also matters and also I prefer to load the image in the child class which has it's own properties such as size.
Does anyone have any idea why this is happening?
kourosh parsa
Ranch Hand

Joined: Jan 22, 2012
Posts: 35
    
    1
By changing the paint event to:

if (image!=null){
try {
while(image.getWidth(null)<1)
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.drawImage(image, 0, 0, null);
}

then it works indicating that there is a race condition problem. Of course that is not a fix, so I welcome any ideas.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

kourosh parsa wrote:
...then it works indicating that there is a race condition problem. Of course that is not a fix, so I welcome any ideas.

Check out MediaTracker


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loading images in Applet inherited class
 
Similar Threads
Output image file and displaying it on screen
Having touble adding graphics to JPanel
how to insert an image using swings in java?
JPanel
JComponent not opaque