I tried importing image via JFileChooser
into JPanel
. It worked. but I need to fit the image inside the panel without losing its aspect ratio or its proportion.
I tried rescaling the image, here's my code:
Image img = Toolkit.getDefaultToolkit().createImage(picture.getSource());
Image scaledImage = img.getScaledInstance(jPanel1.getWidth(),jPanel1.getHeight(),Image.SCALE_SMOOTH);
g2.drawImage(scaledImage, 0, 0,null,null);
But unable to protect its ratio. I need a simple code for this.