Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading images in Applet inherited class

 
kourosh parsa
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic