简体   繁体   English

调整JLabel的ImageIcon的大小

[英]Resize an ImageIcon for an JLabel

I created a class called VainillaImagen: 我创建了一个名为VainillaImagen的类:

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

and then I created a methos called setDimensions that use another method called resizeVainillaImg. 然后我创建了一个名为setDimensions的方法,该方法使用了另一个称为resizeVainillaImg的方法。 But the resizeVainillaImg method dont work any ideas why? 但是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);

}

Although I didn't understand the setDimensions(), but I think you are trying to fit your image into screen width and height. 尽管我不了解setDimensions(),但我认为您正在尝试将图像适合屏幕的宽度和高度。

By multiplying int values of width and height in setDimensions(), you will simply be able to multiply small int numbers. 通过在setDimensions()中乘以width和height的int值,您将能够简单地乘以小的int数。 For bigger numbers you will run out of memory because of huge image size (width screenwidth , height screenheight). 对于更大的数字,由于巨大的图像大小(宽屏宽度高屏高度您将用光内存。

Lets assume you want to resize your image to percent of your screen, or use the default height and with of the image. 假设您想将图像调整为屏幕的百分比,或者使用图像的默认高度和尺寸。 Using the code below, pass negative number (-1 for example) to ignore the screen size, and 0> number to resize it to percent of screen. 使用下面的代码,传递负数(例如-1)以忽略屏幕尺寸,并传递0>数字以将其尺寸调整为屏幕的百分比。

I hope this help. 希望对您有所帮助。 However, it you have some other think in your mind, just remember to use float because of int * int multiplications :) 但是,您还有其他想法,请记住要使用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);
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM