有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用Java实现swing缩放图像

我想使用下面的java代码将图像缩小。原始图像显示在屏幕上(高度大于maxHeight)。为什么

try{

        final int maxHeight = 600;
        JLabel picLabel;
        BufferedImage myPicture = ImageIO.read(new File("src/VIZ/"+listOfFiles[1].getName()));
        int height = myPicture.getHeight();
        int width = myPicture.getWidth();
        if ( height < maxHeight ){
            int multiplier = height/maxHeight;
            height = maxHeight;
            int newWidth = width/multiplier;
            BufferedImage bi = new BufferedImage(newWidth, maxHeight, BufferedImage.TYPE_INT_ARGB);
            picLabel = new JLabel(new ImageIcon( bi ));
        }else{
        picLabel = new JLabel(new ImageIcon( myPicture ));
        }


        console.add(picLabel);
    }

    catch(Exception e){

    }

共 (0) 个答案