如何强制ImageIcon达到一定的大小?

时间:2014-09-11 11:50:02

标签: java image swing imageicon

目前,我使用此代码:

 public class Test extends JFrame {
static Test t = null;
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
              t =  new Test();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public static void addComponentsToPane(Container pane) {

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    ImageIcon icon;
        icon = new ImageIcon(System.getProperty("user.dir") + "/res/background.png");
    Image img = icon.getImage() ;  
      Image newimg = getScaledImage(img, frame.getWidth(), frame.getHeight()) ;  
      icon = new ImageIcon( newimg );
     JLabel background=new JLabel(icon);

        //frame.getContentPane().add(background);

        background.setLayout(new FlowLayout());

    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = frame.getHeight() * 10;
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;

    pane.add(background, c);


}
private static Image getScaledImage(Image srcImg, int w, int h){
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    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;
}

private void createAndShowGUI() {
    frame = new JFrame("GridBagLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

static JFrame frame = null;

public Test() {

    createAndShowGUI();
    addComponentsToPane(frame.getContentPane()); 
    frame.pack();
    frame.setVisible(true);
    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}

}

这适用于按钮,但由于某种原因,对于带有图像的JLabel不起作用。 如何使这个JLabel / Image符合我想要的尺寸?

目前图像非常小, enter image description here 实际上,与JButton相同代码占用的区域更像是这样的: enter image description here 此外,我可以让ImageIcon比按钮4更大,我也想避免。

那么我如何拉伸/收缩图像以适应我希望的区域呢?

2 个答案:

答案 0 :(得分:3)

您可以使用http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/IconDemoProject/src/components/IconDemoApp.java中的代码段创建缩放图片... (来自http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

只需测量按钮并调整图像尺寸

/**
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 * @param srcImg - source image to scale
 * @param w - desired width
 * @param h - desired height
 * @return - the new resized image
 */
private Image getScaledImage(Image srcImg, int w, int h){
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    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;
}

我想从MadProgammer那里得到关于使用scaledInstance的提示:https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

那么如何测量按钮尺寸?

final Button b = new Button(); //create it on your custom way...
b.addComponentListener(new ComponentAdapter() {

    @Override
    public void componentResized(ComponentEvent e){
        int w = b.getWidth();
        int h = b.getHeight();            
        setIconSize(w,h); //TODO by yourself (sorry, if more help required please say so)
    }
});

答案 1 :(得分:1)

  

那么我如何拉伸/收缩图像以适应我希望的区域呢?

查看达里尔的Stretch Icon。图标会自动按照组件的大小进行绘制。