Resize a Picture to Fit a Jlabel

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 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...

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 in java

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

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.

Resizing Image to fit jLabel Trouble

That image has transparency. So change BufferedImage.TYPE_INT_RGB to BufferedImage.TYPE_INT_ARGB

It is not obvious at SO on a white BG, but try this SSCCE & it becomes more clear..

import java.net.URL;
import javax.swing.*;

class ShowImage {

public static void main(String[] args) throws Exception {
final URL url = new URL("http://i.stack.imgur.com/1yeUy.png");
Runnable r = new Runnable() {
@Override
public void run() {
JLabel l = new JLabel(new ImageIcon(url));

JOptionPane.showMessageDialog(null, l);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}

Sample Image

Is it possible to resize a Jlabel?

You just need to set the icon on the JLabel, the rest should just follow without any other code.

See this example:

Demo picture

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestResizingLabel {

protected void initUI() throws MalformedURLException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
final ImageIcon originalImage = new ImageIcon(new URL(
"http://mgl.skyrock.net/big.138643852.jpg?78138742"));
final ImageIcon scaledImage = new ImageIcon(originalImage.getImage()
.getScaledInstance(originalImage.getIconWidth() / 4,
originalImage.getIconHeight() / 4, Image.SCALE_SMOOTH));
final JLabel label = new JLabel(scaledImage);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
System.err.println("in");
label.setIcon(originalImage);
}

@Override
public void mouseExited(MouseEvent e) {
label.setIcon(scaledImage);
}
});
panel.add(label);
frame.add(panel);
frame.setSize(600, 600);
frame.setVisible(true);
}

/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new TestResizingLabel().initUI();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}

}


Related Topics



Leave a reply



Submit