How to Resize Jlabel Imageicon

How to resize JLabel ImageIcon?

Resizing the icon is not straightforward. You need to use Java's graphics 2D to scale the image. The first parameter is a Image class which you can easily get from ImageIcon class. You can use ImageIcon class to load your image file and then simply call getter method to get the image.

private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();

return resizedImg;
}

Resize a picture to fit a JLabel

Outline

Here are the steps to follow.

  • Read the picture as a BufferedImage.
  • Resize the BufferedImage to another BufferedImage that's the size of the JLabel.
  • Create an ImageIcon from the resized BufferedImage.

You do not have to set the preferred size of the JLabel. Once you've scaled the image to the size you want, the JLabel will take the size of the ImageIcon.

Read the picture as a BufferedImage

BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
e.printStackTrace();
}

Resize the BufferedImage

Image dimg = img.getScaledInstance(label.getWidth(), label.getHeight(),
Image.SCALE_SMOOTH);

Make sure that the label width and height are the same proportions as the original image width and height. In other words, if the picture is 600 x 900 pixels, scale to 100 X 150. Otherwise, your picture will be distorted.

Create an ImageIcon

ImageIcon imageIcon = new ImageIcon(dimg);

Resizing JLabel ImageIcon with HTML

This basically requires you to have a URL reference to the image in question. This is quite easy to achieve if the image is an embedded resource (as demonstrated below), but you should be able to generate a URL from file using File#getURI#toURL

Sample Image

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class HTMLLabel {

public static void main(String[] args) {
new HTMLLabel();
}

public HTMLLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

URL url = getClass().getResource("/Chicken.png");
System.out.println(url);

JLabel perLabel = new JLabel("<html><img width=125 height=125 src=" + url + "></html>");
JLabel orgLabel = new JLabel("<html><img src=" + url + "></html>");

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;

frame.add(orgLabel, gbc);
frame.add(perLabel, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

}

It should be noted that scaling is pretty redumentry. I would, personally, avoid it and simple devise some kind of ImagePane that had better scaling capabilities, for example

How to adjust Image size inside jLabel in JAVA Netbeans?

You can use the Image.getScaledInstance(...) method to scale the image to your desired size.

Then you create an ImageIcon using the image and add the Icon to the JLabel.

Edit:

You can also try using Darryl's Stretch Icon. Using this class the Icon will resize dynamically as frame resizes if you use an appropriate layout manager.

Autoresize the image using imageicon on jlabel

you can scale your image the following way,

    Image img = format.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH);
jLabel15.setIcon(new ImageIcon(img));

I have scalled the image to 50X50 you can scale it to your desired size

Resizing image to fit in JLabel

BufferedImage img = ImageIO.read(...);
Image scaled = img.getScaledInstance(500, 500, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(scaled);

Beware, that this will scale the image so that it is square. Take a look at Java: maintaining aspect ratio of JPanel background image which discusses maintaining the aspect ratio of the image when scaled.

Also, you should read The Perils of Image.getScaledInstance() and have a look at Scale the ImageIcon automatically to label size which uses a divide and conqure scaling algorithim and Quality of Image after resize very low -- Java which demonstrates the issues of doing a one step scale...

Resize a picture to fit a JLabel in java

You can check out Darryl's Stretch Icon. The image will automatically be scaled to fill the space available to the label.



Related Topics



Leave a reply



Submit