devxlogo

Image loading

Image loading

Question:
I’m having trouble reshaping a frame to the size of a loaded image. I start out by saying:

    int fx = image.getWidth(this);    int fy = image.getHeight(this);then I try to apply it to the frame    this.resize(fx, fy);

But, to my misfortune, the frame totally collapsesto nothing but the title bar. What am I doing wrong?

Answer:
More than likely, either the image was not loaded properly, or it has not finished loading. You can check this by printing out the width and height values. If they are 0, the image isn’t loaded. Since your frame is collapsing after the resize, the image dimensions are almost certainly 0. To make sure an image has finished loading, you must use a java.awt.MediaTracker. There is a very good example in the javadocs for MediaTracker on how to do this, so I won’t go into the details here.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist