I created a method to resize the image from project resource package which holds the images and icons I use. However, it doesn't seem to render it or resize it smoothly.
public Image getResourceImage(String filepath){
Image img = new ImageIcon(getClass().getClassLoader().getResource(filepath)).getImage();
return img;
/* Example usage:
Image loginBg = new ImageMethod().getAssetImage("foldername/filename.extension");
*/
}
Below are the original png images.
And below are the resized version which I'm using for the login panel.
It looks pixelated. Is there any way to achieve something as clear and smooth like this considering the original icon dimensions is 512x512
The icons are all in PNG format.
I've limited knowledge with Images in Java so I'd appreciate any help or suggestion.
Thanks.
I've finally solved it. Created 3 versions of my method.
public Image getResourceImage(String filepath){
Image img = new ImageIcon(getClass().getClassLoader().getResource(filepath)).getImage();
return img;
/* Example usage:
Image loginBg = new ImageMethod().getAssetImage("foldername/filename.extension");
*/
}
public ImageIcon getResourceImage(String filepath, JLabel label){
Image rawImage = new ImageIcon(getClass().getClassLoader().getResource(filepath)).getImage();
Image renderedImage = rawImage.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(renderedImage);
return image;
}
public Image getResourceImage(String filepath, JPanel panel){
Image rawImage = new ImageIcon(getClass().getClassLoader().getResource(filepath)).getImage();
Image renderedImage = rawImage.getScaledInstance(panel.getWidth(), panel.getHeight(), Image.SCALE_SMOOTH);
return renderedImage;
}
This will definitely help the others. Thanks for the advice Obicere.