【问题标题】:How to resize JLabel ImageIcon?如何调整 JLabel ImageIcon 的大小?
【发布时间】:2023-04-01 13:15:01
【问题描述】:

我正在制作一个具有以下布局 (MigLayout) 的 Java Swing 应用程序:

[icon][icon][icon][....]
where icon = jlabel and the user can add more icons

当用户添加或删除图标时,其他图标应该缩小或增长。

我的问题很简单:我有一个JLabel,其中包含一个ImageIcon;如何调整此图标的大小?

【问题讨论】:

标签: java image swing


【解决方案1】:

我发现 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

【讨论】:

    【解决方案2】:

    这将保持正确的纵横比。

        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));
        }
    

    【讨论】:

      【解决方案3】:

      然后呢?:

      ImageIcon imageIcon = new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
      label.setIcon(imageIcon);
      

      发件人:Resize a picture to fit a JLabel

      【讨论】:

        【解决方案4】:

        调整图标大小并不简单。您需要使用 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;
        }
        

        【讨论】:

          【解决方案5】:

          试试这个:

          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
          

          (找到它here

          【讨论】:

            【解决方案6】:

            我同意此代码有效,可以从文件中调整 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));
            }
            

            【讨论】:

              【解决方案7】:

              一种(快速而肮脏的)方法来调整图像的大小以使用 HTML 并在图像元素中指定新的大小。这甚至适用于具有透明度的动画图像。

              【讨论】:

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