[英]How to resize JLabel ImageIcon?
我正在制作具有以下布局( MigLayout
)的 Java Swing 应用程序:
[icon][icon][icon][....]
where icon = jlabel and the user can add more icons
当用户添加或删除图标时,其他图标应该缩小或增长。
我的问题很简单:我有一个包含ImageIcon
的JLabel
; 如何调整此图标的大小?
尝试这个:
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 图形来缩放图像。 第一个参数是图像 class,您可以从ImageIcon
class 轻松获得。 您可以使用ImageIcon
class 加载图像文件,然后只需调用 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);
来自: 调整图片大小以适合 JLabel
这将保持正确的纵横比。
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 和 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.