A part of my application is working as a tutorial. For that purpose I got JPanels that display a JLabel that has an image as content. Although when the image is larger than what fits the screen it will just cut what doesn’t fit. What I need to do is resize the image so that I will fit the space the JLabel is given. Also tried using JScrollPanes but didn’t make any difference(although I prefer to resize image).
I tried getting a scaled instance of the image using
Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
but it didn’t make any difference.
This is the current code :
private JLabel getTutorialLabel(int type) {
String path = "/Images/Tutorial/test" + type + ".png";
try {
BufferedImage tutorialPic = ImageIO.read(getClass().getResource(path));
// int height = (int) (screenDim.height * 0.9);
// int width = (int) (screenDim.width * 0.9);
// Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(tutorialPic));
return picLabel;
} catch (IOException ex) {
Logger.getLogger(Tutorial.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
How I get the image to display properly?
Please note that there are other components displayed along with the JLabel(JMenuBar and some buttons on bottom of screen) so I need to image to fill the space JLabel is given.
Edit: updated the method
You could try this method in the Java Helper Library.
Or, here’s the method itself: