How to adjust the size of Image / IconImage in JLabel?

This is my code:

String s = "/Applications/Asphalt6.app";
JFileChooser chooser = new JFileChooser();

File file = new File(s);
Icon icon = chooser.getIcon(file);

// show the icon
JLabel ficon = new JLabel(s,icon,SwingConstants.LEFT);

Now, the image extracted from the icon is very small How can I adjust it?

Solution

import java.awt.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;

class BigIcon {

    public static void main(String[] args) {
        JFileChooser chooser = new JFileChooser();
        File f = new File("BigIcon.java");
        Icon icon = chooser.getIcon(f);

        int scale = 4;

        BufferedImage bi = new BufferedImage(
            scale*icon.getIconWidth(),scale*icon.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        g.scale(scale,scale);
        icon.paintIcon(null,g,0);
        g.dispose();

        JOptionPane.showMessageDialog(
            null,new JLabel(new ImageIcon(bi)));
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>