【问题标题】:Resize an ImageIcon for an JLabel为 JLabel 调整 ImageIcon 的大小
【发布时间】:2015-05-01 13:48:41
【问题描述】:

我创建了一个名为 VainillaImagen 的类:

public VainillaImage(String url){
    this.icimg=new ImageIcon(url);
    this.imagen=new JLabel(this.icimg);
    this.imagen.setVisible(true);
}

然后我创建了一个名为 setDimensions 的方法,它使用另一种名为 resizeVainillaImg 的方法。但是 resizeVainillaImg 方法没有任何想法,为什么?

public void setDimensions(boolean wRel,int width,boolean hRel,int height){
    Dimension dimPantalla = Toolkit.getDefaultToolkit().getScreenSize();
    int nwidth,nheight;
    if(wRel){
        nwidth=(int)(width*(dimPantalla.width));
    }else{
        nwidth=width;
    }
    if(hRel){
        nheight=(int)(height*(dimPantalla.height));
    }else{
        nheight=height;
    }
    resizeVainillaImg(nwidth,nheight);
}


public void resizeVainillaImg(int newWidth,int newHeight){
    Image img = this.icimg.getImage();
    BufferedImage bi = new BufferedImage(newWidth,newHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(img, 0, 0, newWidth, newHeight,null);
    g.dispose();
    this.icimg = new ImageIcon(bi);
    this.imagen.setIcon(this.icimg);

}

【问题讨论】:

    标签: java image swing resize imageicon


    【解决方案1】:

    虽然我不明白 setDimensions(),但我认为您正在尝试将图像适应屏幕宽度和高度。

    通过在 setDimensions() 中将宽度和高度的 int 值相乘,您将能够简单地将小的 int 数相乘。对于更大的数字,由于图像尺寸过大(宽度screenwidth,heightscreenheight),您将耗尽内存。

    假设您要将图像调整为屏幕的百分比,或使用图像的默认高度和 with。使用下面的代码,传递负数(例如 -1)以忽略屏幕大小,传递 0> 数字以将其调整为屏幕的百分比。

    我希望这会有所帮助。但是,如果您有其他想法,请记住使用 float 因为 int * int 乘法:)

    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    
    /**
     *
     * @author Pasban
     */
    public class VainillaImage {
    
        private ImageIcon icimg;
        private JLabel imagen;
    
        public static void main(String args[]) {
            JDialog d = new JDialog();
            VainillaImage v = new VainillaImage("92-1024x576.jpg");
            d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            d.getContentPane().add(v.imagen);
            v.setDimensions(-1, 1);
            d.pack();
            d.setLocationRelativeTo(null);
            d.setVisible(true);
        }
    
        public void setDimensions(double width, double height) {
            Dimension dimPantalla = Toolkit.getDefaultToolkit().getScreenSize();
            int nwidth, nheight;
            nwidth = (int) (width * (dimPantalla.width));
            nheight = (int) (height * (dimPantalla.height));
            resizeVainillaImg(nwidth, nheight);
        }
    
        public void resizeVainillaImg(int newWidth, int newHeight) {
            Image img = this.icimg.getImage();
            newWidth = Math.max(newWidth, img.getHeight(null));
            newWidth = Math.max(newHeight, img.getHeight(null));
            BufferedImage bi = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(img, 0, 0, newWidth, newHeight, null);
            g.dispose();
            this.icimg = new ImageIcon(bi);
            this.imagen.setIcon(this.icimg);
        }
    
        public VainillaImage(String url) {
            this.icimg = new ImageIcon(url);
            this.imagen = new JLabel(this.icimg);
            this.imagen.setVisible(true);
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您尝试根据标签的可用空间动态调整图标大小,请查看 Darryl 的 Stretch Icon

      【讨论】:

        猜你喜欢
        • 2023-04-01
        • 2013-08-23
        • 1970-01-01
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        • 2020-11-30
        • 1970-01-01
        相关资源
        最近更新 更多