Displaying and manipulating an BufferedImage

I have to program a game in Java for a university course. Therefore I need to display a quite big image (the game field).

I tried to display the image with an own Panel Class ImagePanel:

publicclass ImagePanelextends JPanel{

private BufferedImage background;

/** Creates a new instance of ImagePanel */

public ImagePanel(BufferedImage image){

this.setBackground(image);

}

publicvoid paintComponent(Graphics graphic){

super.paintComponent(graphic);

graphic.drawImage(getBackground(), 0,0,getBackground().getWidth(),getBackground().getHeight(),this);

}

public Dimension getPrefferedSize(){

returnnew Dimension(getBackground().getWidth(),getBackground().getHeight());

}

public BufferedImage getBackground(){

return background;

}

publicvoid setBackground(BufferedImage background){

this.background = background;

}

}

I display a BufferedImage using the ImagePanel on a JScrollPane. The image is displayed, but the scrollbars doesn't appear (the Horizontal and VerticalScrollBarPolicy is set to AS_NEEDED) :

BufferedImage map = ImageIO(UrlToFile);

Graphics2D graphic = map.createGraphics();

graphic.drawRenderedImage(map,null);

imagePanel =new ImagePanel(map);

jScrollPane1.add(imagePanel);

jScrollPane1.getViewport().setView(imagePanel);

I also want to resize the image from time to time and put another image on the image. Can anyone tell me how to display the image, scroll and resize it? I also have to put other (small) images on the map, is graphic.DrawRenderedImage(buffImage,null) the best way to do this?

[2609 byte] By [Flotuetea] at [2007-11-26 19:04:21]
# 1

First read How to Use Scroll Panes:

http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html

You don't add your ImagePanel to the scroll pane, you set the client using the scroll pane constructor or setViewportView.

Also you need to set the preferred size of the scroll pane to be smaller than the preferred size of your ImagePanel if you want to see the scroll bars.

If you want to resize your image you can set a new preferred size for your ImagePanel, and then call ImagePanel.revalidate.

You just need to change the ImagePanel a bit so it will scale the image to its preferred size.

public class ImagePanel extends JPanel {

private BufferedImage background;

/** Creates a new instance of ImagePanel */

public ImagePanel(BufferedImage image) {

this.setBackground(image);

setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));

}

public void paintComponent(Graphics graphic) {

super.paintComponent(graphic);

graphic.drawImage(getBackground(), 0,0,getPreferredSize().getWidth(),getPreferredSize().getHeight(), this);

}

public BufferedImage getBackground() {

return background;

}

public void setBackground(BufferedImage background) {

this.background = background;

}

}

If you want to draw on top of your image you can use one of the drawImage functions in [url http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics2D.html]Graphics2D[/url].

Rodney_McKaya at 2007-7-9 20:52:57 > top of Java-index,Security,Cryptography...
# 2

Thank you very much, the Scrollbars are showing up now. I changed the code to add the imagePanel to:

imagePanel = new ImagePanel(karte);

jScrollPane1.add(imagePanel);

jScrollPane1.setViewportView(imagePanel);

jScrollPane1.setPreferredSize(new Dimension(100,100));

The only thing I haven't solved is, that the ScrollPane doesn't notice the Size change of the imagePanel when i resized the background image. Perhaps you can tell me what I have to do with the ScrollPane to make it notice the size change.

Flotuetea at 2007-7-9 20:52:58 > top of Java-index,Security,Cryptography...
# 3
It's written in my previous reply.You have to call imagePanel.revalidate() after setting the new preferred size.
Rodney_McKaya at 2007-7-9 20:52:58 > top of Java-index,Security,Cryptography...