Creating and displaying a BufferedImage

Ok, so I've created a little self-contained piece of code that ought to display a BufferedImage that has been created from a URL. I believe I need to use a BufferedImage because I'm going to be doing a number of real-time affine transforms, and need the rendering to be fast and smooth.

I create an ImageIcon from the URL, then create a BufferedImage from the ImageIcon. I then place the BufferedImage in a custon JPanel that overwrites paintComponent(). Just as a check, that method also prints out the size of the image, which is indeed the correct image size, so I believe the BufferedImage is being created correctly.

However, when I run the program no image is displayed. Any advice would be greatly appreciated.

My code:

publicstaticvoid main(String[] args){

try{

URL url =new URL("http://www.gigapxl.org/images/StHelensTrees800.jpg");

drawImage(url);

}catch (MalformedURLException e){

e.printStackTrace();

}

}

publicstaticvoid drawImage(URL location){

JFrame frame =new JFrame();

try{

ImageIcon icon =new ImageIcon(location);

if (icon.getImageLoadStatus() != MediaTracker.COMPLETE){

System.out.println("not complete");

thrownew Exception();

}

Image image = icon.getImage();

BufferedImage bImage =new BufferedImage(

image.getWidth(null),

image.getHeight(null),

BufferedImage.TYPE_INT_ARGB);

BufferedImageJPanel bComponent =new BufferedImageJPanel(bImage);

frame.add(bComponent);

frame.pack();

frame.setVisible(true);

bComponent.validate();

bComponent.repaint();

}catch (Exception e){

e.printStackTrace();

}

}

The BufferedImageJPanel class:

publicclass BufferedImageJPanelextends JPanel{

private BufferedImage bImage;

public BufferedImageJPanel(BufferedImage bImage){

this.bImage = bImage;

}

publicvoid paintComponent(Graphics g){

super.paintComponent(g);

System.out.println("painting");

Graphics2D g2 = (Graphics2D)g;

g2.drawImage(bImage, null, 0, 0);

System.out.println("image size: " + bImage.getWidth());

}

}

Again, any thoughts on what I'm doing wrong will be appreciated!

Thanks,

Sam

[3878 byte] By [Asbestosa] at [2007-11-27 2:50:36]
# 1
Use a label to display the image. [url http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html]How to Use Icons[/url]
camickra at 2007-7-12 3:23:02 > top of Java-index,Desktop,Core GUI APIs...
# 2

> Use a label to display the image.

I know how to display an ImageIcon. But when I tried to perform affine transformations on the ImageIcon the rendering was extremely slow. From what I've read, I need to be performing these transformations on a BufferedImage, not an ImageIcon.

Or are you saying I should do my processing on the BufferedImage, transform the BufferedImage into an ImageIcon, and then display it?

Asbestosa at 2007-7-12 3:23:02 > top of Java-index,Desktop,Core GUI APIs...
# 3
HiThis will show you how: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.htmlRegardsPaul
paul_gaina at 2007-7-12 3:23:02 > top of Java-index,Desktop,Core GUI APIs...
# 4

Your original question was about "displaying" a buffered image, it said nothing about "transforming" a buffered image, so I gave you the easiest solution.

If you want to manipulate the buffered image then you will need to paint it yourself. When you do custom painting you need to specify the "preferred size" of the component. Since you didn't the size would be (0, 0). Don't know if this is the only problem but try setting the preferred size of the panel to the size of the image.

camickra at 2007-7-12 3:23:02 > top of Java-index,Desktop,Core GUI APIs...
# 5

>This will show you how:

>http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html

Hi Paul, thanks for the link. I was about to reply saying that I already had that part down, but then I discovered that I was never painting my Image to my BufferedImage! The trials of trying to re-factor code into a self-contained example... Anyhow, it works now with the addition of the lines

Graphics2D g2 = bImage.createGraphics();

g2.drawImage(image, 0, 0, null);

g2.dispose();

in the drawImage method.

> Your original question was about "displaying" a

> buffered image, it said nothing about "transforming"

> a buffered image, so I gave you the easiest

> solution.

(actually, I mentioned it in my first paragraph, but I guess I didn't repeat it, so no matter!).

> If you want to manipulate the buffered image then you

> will need to paint it yourself. When you do custom

> painting you need to specify the "preferred size" of

> the component. Since you didn't the size would be (0,

> 0). Don't know if this is the only problem but try

> setting the preferred size of the panel to the size

> of the image.

It seems to work now ever without setting any prefferedSize, I assume because I already set the size of the BufferedImage when I create it.

Anyhow, it works now. Thanks for your help!

Asbestosa at 2007-7-12 3:23:02 > top of Java-index,Desktop,Core GUI APIs...