Why is the following bit of code returns Height: -1 which means that the height is yet not known. How to get height of the image?
try {
// Create a
Toolkit#createImage(...)
is non-blocking. Generally I would rather use javax.imageio.ImageIO
to read images.
To wait for the Toolkit#createImage(...)
, use:
MediaTracker mediaTracker = new MediaTracker(component);
mediaTracker.addImage(image);
try {
mediaTracker.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
After that, you can call image.getHeight()