Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem displaying images

 
Kory Spansel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic