File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Problem displaying images

Kory Spansel
Greenhorn

Joined: Mar 05, 2002
Posts: 6
Hi,
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
Kory
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 ;
pMain.setBackground(color2);
pMain.setLayout(null);
ImageIcon icon = null;
try
{
icon = new ImageIcon(new URL (MyApplet.getCodebase() +"pictures/sun.JPG"));
}
catch(MalformedURLException mue){}

lbTest = new JLabel("THIS IS MY PIC", icon, JLabel.CENTER);
lbTest.setVerticalTextPosition(JLabel.BOTTOM);
lbTest.setHorizontalTextPosition(JLabel.CENTER);
lbTest.setBackground(color1);
lbTest.setBounds(new Rectangle (30, 20, 740,400));
pMain.add(lbTest);
}
And for detail, here is a good link for swing components:
http://java.sun.com/docs/books/tutorial/uiswing/components/components.html
Kory Spansel
Greenhorn

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.
Kory
Nathan Pruett
Bartender

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...







-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
Don't get me started about those stupid light bulbs.
 
subject: Problem displaying images
 
Similar Threads
Image+Button+applet
drawImage not getting the image drawn before printing!
Copy Picture (Image)
getCodeBase problem
Problem Loading Image in Applet