0

I'm creating a simple java program to load a image and zoom in/out the image. So far I have been able load the picture and zoom it. But when drawing the zoomed out image I can still see the previous image on the JPanel.

Code for loading the image

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Images(jpg,png,gif)", "jpg","png","gif"));
int opt = fileChooser.showOpenDialog(this);
if(opt  == JFileChooser.APPROVE_OPTION){
    String filePath = fileChooser.getSelectedFile().getAbsolutePath();
    try {
        bufImage = ImageIO.read(fileChooser.getSelectedFile());
    } catch (IOException ex) {
        Logger.getLogger(ImageZoom.class.getName()).log(Level.SEVERE, null, ex);
    }
    image = new ImageIcon(filePath);
    imgWidth = image.getIconWidth();
    imgHeight = image.getIconHeight();

    imagePanel.getGraphics().drawImage(image.getImage(), 0, 100, image.getIconWidth(), image.getIconHeight(), imagePanel);
}

Code for zooming in

double scale_factor = 1.5;
imagePanel.getGraphics().drawImage(image.getImage(), 0, 100, (int)(imgWidth*=scale_factor), (int)(imgHeight*=scale_factor), imagePanel);

Code for zooming out

double scale_factor = 0.5;
imagePanel.getGraphics().drawImage(image.getImage(), 0, 100, (int)(imgWidth*=scale_factor), (int)(imgHeight*=scale_factor), imagePanel);  

Can some one please suggest a way to only have the resized image on the panel?
This is how zooming out looks like. same problem occurs when opening a new picture.
I tried repainting the panel but then everything vanishes.

This is how zooming out looks like

Isuru Pathirana
  • 1,060
  • 1
  • 16
  • 37

1 Answers1

2

That's because imagePanel.getGraphics() isn't how custom painting works in Swing. Instead, create a custom class which extends from something like JPanel and override it's paintComponent method, painting your scaled image there (making sure to call super.paintComponent first)

See Painting in AWT and Swing and Performing Custom Painting for more details

For example:

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I tried what you said and I cannot see my image on the panel. Here is the code I used : http://paste.ubuntu.com/11952468/ Can you please check it and let me know what is the mistake I made – Isuru Pathirana Jul 28 '15 at 09:03
  • Your draw panel should be a separate component, you currently have a bunch of other components on your draw panel which are likely painting over the image – MadProgrammer Jul 28 '15 at 09:06
  • It's also likely you have a NullPointException, as the Image won't have been loaded before the components Tis painted. You should also not be calling repaint directly or indirectly from within any paint method – MadProgrammer Jul 28 '15 at 09:08
  • If not to call repaint how should I redraw the image when I'm loading a new image? And I'm not getting a NullPointerException. Can you please give me a example? – Isuru Pathirana Jul 28 '15 at 09:12
  • You can call repaint, just not from within any `paint` methods, otherwise you set up an infinite loop which will consume your CPU cycles. I've already linked 3 examples – MadProgrammer Jul 28 '15 at 09:49
  • Thanks for the help. I managed to get it fixed. Here is the working code http://paste.ubuntu.com/11952695/ – Isuru Pathirana Jul 28 '15 at 10:05