It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Why isn't my image showing up? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why isn Watch "Why isn New topic

Why isn't my image showing up?

Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
I've create a class that extends Image and has in it a private BufferedImage object that it draws on and defers to it for any method calls. For example:

What i draw on the buffered image is a bunch of images that I load and lay out on the buffered image in a specific design. I've checked the following and they all check out as correct:

1. the buffered image width,height
2. every image is painted within the bufferedimage's width/height (for x,y)
3. every image does exist

For some reason, however, when i try to pass the image to a JLabel, via new ImageIcon( new MyImageClass() ), and add that to a JFrame's content pane, it always comes as an empty JFrame. It does get sized properly, however.

In checking which "Image" methods are called, only two are ever called (from what i can tell):

public int getWidth(ImageObserver o)
public int getHeight(ImageObserver o)

Why is that? It seems to me that to draw an image, a component would need to grab it's pixel data somehow. Any idea why it never calls any other methods? (BTW, when it does call those methods, it prints out the correct 160, 179 values).


More Info
* the images are all "png" images with some transparency
* I create the BufferedImage as:
BufferedImage b = new BufferedImage( w, h, BufferedImage.TYP_INT_RGB );
[ October 01, 2006: Message edited by: Dan Bizman ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
When I tried what you describe new ImageIcon( new MyImageClass() ) I get an exception in the console:

This works okay:

Another approach is to use composition (vis–a–vis inheritance):
I agree. Here's the link:
subject: Why isn't my image showing up?
It's not a secret anymore!