我正在制作一个Java Swing应用程序,其布局如下(使用MigLayout
):
[icon][icon][icon][....]
where icon = jlabel and the user can add more icons
当用户添加或删除图标时,其他图标应缩小或增大。我的问题非常简单:我有一个包含ImageIcon的JLabel;如何调整此图标的大小?
我正在制作一个Java Swing应用程序,其布局如下(使用MigLayout
):
[icon][icon][icon][....]
where icon = jlabel and the user can add more icons
当用户添加或删除图标时,其他图标应缩小或增大。试试这个:
ImageIcon imageIcon = new ImageIcon("./img/imageName.png"); // load the image to a imageIcon
Image image = imageIcon.getImage(); // transform it
Image newimg = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
imageIcon = new ImageIcon(newimg); // transform it back
(发现它在这里)
调整图标的大小并不是一件简单的事情。您需要使用Java的2D图形来缩放图像。第一个参数是一个Image类,您可以轻松地从ImageIcon
类中获取它。您可以使用ImageIcon
类来加载您的图像文件,然后仅需调用getter方法即可获取图像。
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
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;
}
那又怎样呢?:
ImageIcon imageIcon = new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
label.setIcon(imageIcon);
public ImageIcon scaleImage(ImageIcon icon, int w, int h)
{
int nw = icon.getIconWidth();
int nh = icon.getIconHeight();
if(icon.getIconWidth() > w)
{
nw = w;
nh = (nw * icon.getIconHeight()) / icon.getIconWidth();
}
if(nh > h)
{
nh = h;
nw = (icon.getIconWidth() * nh) / icon.getIconHeight();
}
return new ImageIcon(icon.getImage().getScaledInstance(nw, nh, Image.SCALE_DEFAULT));
}
一种(快速而简单的)缩放图像的方法是使用HTML并在图像元素中指定新大小。即使对于具有透明度的动画图像,这也可以起作用。
我同意这段代码是有效的,用于从文件中调整ImageIcon的大小以便在保持纵横比的同时进行展示。我已经使用了以下代码:
/*
* source File of image, maxHeight pixels of height available, maxWidth pixels of width available
* @return an ImageIcon for adding to a label
*/
public ImageIcon rescaleImage(File source,int maxHeight, int maxWidth)
{
int newHeight = 0, newWidth = 0; // Variables for the new height and width
int priorHeight = 0, priorWidth = 0;
BufferedImage image = null;
ImageIcon sizeImage;
try {
image = ImageIO.read(source); // get the image
} catch (Exception e) {
e.printStackTrace();
System.out.println("Picture upload attempted & failed");
}
sizeImage = new ImageIcon(image);
if(sizeImage != null)
{
priorHeight = sizeImage.getIconHeight();
priorWidth = sizeImage.getIconWidth();
}
// Calculate the correct new height and width
if((float)priorHeight/(float)priorWidth > (float)maxHeight/(float)maxWidth)
{
newHeight = maxHeight;
newWidth = (int)(((float)priorWidth/(float)priorHeight)*(float)newHeight);
}
else
{
newWidth = maxWidth;
newHeight = (int)(((float)priorHeight/(float)priorWidth)*(float)newWidth);
}
// Resize the image
// 1. Create a new Buffered Image and Graphic2D object
BufferedImage resizedImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
// 2. Use the Graphic object to draw a new image to the image in the buffer
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, 0, 0, newWidth, newHeight, null);
g2.dispose();
// 3. Convert the buffered image into an ImageIcon for return
return (new ImageIcon(resizedImg));
}
我发现trolologuy在代码的最后一行做了一个小修改,你需要实现一个新的ImageIcon才能使代码正确编译(是的,我知道这已经是10年前的事情了)。我认为这是一个简单的修复方法,但Suken Shah和Mr. Polywhirl有更好的整体修复方案。
ImageIcon imageIcon = new ImageIcon("./img/imageName.png"); // assign image to a new ImageIcon
Image image = imageIcon.getImage(); // transform it
Image newimg = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH); // scale it smoothly
ImageIcon newImageIcon = new ImageIcon(newimg); // assign to a new ImageIcon instance