为JLabel调整ImageIcon的大小

时间:2015-05-01 13:48:42

标签: java image swing resize imageicon

我创建了一个名为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);

}

2 个答案:

答案 0 :(得分:0)

虽然我不理解setDimensions(),但我认为你试图将你的图像调整到屏幕宽度和高度。

通过在setDimensions()中乘以宽度和高度的int值,您将能够简单地乘以小的int数。对于较大的数字,由于图像尺寸较大(宽度屏幕宽度,高度屏幕高度),因此内存不足。

让我们假设你想要将你的图像大小调整到屏幕的百分比,或者使用默认的高度和图像的高度。使用下面的代码,传递负数(例如-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);
    }
}

答案 1 :(得分:0)

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