File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Problem displaying images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem displaying images" Watch "Problem displaying images" New topic

Problem displaying images

Kory Spansel

Joined: Mar 05, 2002
Posts: 6
I am having problems loading a displaying images on my JPanel. What i have is a Card class that loads a displays a card image:

The problems is that nothing shows up. When I call cardImage.getWidth(), and getHeight I get -1 and -1. I thought it was because the image had not yet been loaded, which is why I added the MediaTracker but I still get nothing. Is it possible that since I am passing in a reference to a parent JPanel that there are problems with the MediaTracker? Thanks
Philip Pross
Ranch Hand

Joined: Jan 17, 2001
Posts: 76
This should help you out, I myself had the same problem.
public void createPanel()
JLabel lbTest ;
ImageIcon icon = null;
icon = new ImageIcon(new URL (MyApplet.getCodebase() +"pictures/sun.JPG"));
catch(MalformedURLException mue){}

lbTest = new JLabel("THIS IS MY PIC", icon, JLabel.CENTER);
lbTest.setBounds(new Rectangle (30, 20, 740,400));
And for detail, here is a good link for swing components:
Kory Spansel

Joined: Mar 05, 2002
Posts: 6
No that's not really what I'm looking for, thanks tho. I need to do it the way I have outlined, but I don't think that my images are correctly being loaded. I thought that when I called the waitForID method of the MediaTracker class it would start loading the image. However, it doesn't seem to be doing anything at all.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

I've tested out your code and you are doing everything correctly (at least in the code you posted...) It looks like the only thing that might be wrong is that you are not passing the correct image file name to the Card.loadImage() method, or the image is not in a path where the program can locate it... if no image file is found a null image is returned with a size of -1 x -1...

Here is the code I used to test this out... if the file named "MyImage.gif" was somewhere where the program could find it (like in the same directory as the class), everything worked...


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link:
subject: Problem displaying images
It's not a secret anymore!