| Author |
Loading images in Applet inherited class
|
kourosh parsa
Greenhorn
Joined: Jan 22, 2012
Posts: 19
|
|
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
Greenhorn
Joined: Jan 22, 2012
Posts: 19
|
|
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: 8436
|
|
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
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
 |
|
|
subject: Loading images in Applet inherited class
|
|
|