I'm using a Jlabel with an icon in swing. I chose an image from my computer as the icon and I can't find a way to make it resize automatically to the size of the Jlabel. Because the image I want is much bigger then the size of the Jlabel i'm only seeing a small part of it. Is there any way to solve this problem so I won't need to resize each image before importing it to the project?
            Asked
            
        
        
            Active
            
        
            Viewed 1,715 times
        
    1 Answers
4
            
            
        Might be your LayoutManger/lack there of thats interfering as JLabel should return correct size according to the content displayed.
Please post SSCCE to show specific problems.
Though regardless a large image will still need to be scaled, here is a nice method I usually use:
public static BufferedImage scaleImage(int w, int h, BufferedImage img) throws Exception {
    BufferedImage bi;
    bi = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
    Graphics2D g2d = (Graphics2D) bi.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
    g2d.drawImage(img, 0, 0, w, h, null);
    g2d.dispose();
    return bi;
}
here is an example:

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Test {
    public Test() {
        initComponents();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
    private void initComponents() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BufferedImage img = null;
        try {
            BufferedImage tmp = ImageIO.read(new URL("http://photos.appleinsider.com/12.08.30-Java.jpg"));
            img = scaleImage(200, 200, tmp);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        JLabel label = new JLabel(new ImageIcon((Image) img));
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }
    public static BufferedImage scaleImage(int w, int h, BufferedImage img) throws Exception {
        BufferedImage bi;
        bi = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(img, 0, 0, w, h, null);
        g2d.dispose();
        return bi;
    }
}
        David Kroukamp
        
- 36,155
 - 13
 - 81
 - 138
 
- 
                    1Don't forget to `g2d.dispose()`. Also, I was pondering how you end up with an `Image` as opposed to a `BufferedImage` in `scaleImage(int w, int h, Image img)` - the reason being I'd tend to `bi = new BufferedImage(w, h, img.getType());` (not possible with an `Image`). – Andrew Thompson Dec 17 '12 at 01:55
 - 
                    @AndrewThompson aah yes `dispose()` I always forget that.. Whats the difference if i omit the call or not? And thank you I forget at 1 stage I had changed Image to `BufferedImage` but not in the example I had above :). and +1 for `img.getType()`. – David Kroukamp Dec 17 '12 at 05:57
 - 
                    1Basically, a `Graphics` instance is apparently a sizable chunk of memory. So if we create a `Graphics`, best to dispose of it as soon as it is done with. I think @trashgod was better at explaining it.. – Andrew Thompson Dec 17 '12 at 06:48
 - 
                    1Good catch by @Andrew. The impact varies by platform; [profiling](http://stackoverflow.com/a/6310284/230513) may offer insight; a few other candidates are listed [here](http://stackoverflow.com/a/2486200/230513). – trashgod Dec 17 '12 at 07:17