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

