gpt4 book ai didi

Java - 如何自动设置 img.getScaledInstance( ); 的宽度?

转载 作者:行者123 更新时间:2023-12-01 16:56:10 24 4
gpt4 key购买 nike

如果图像太大,我想调整图像的大小,但我希望它保持其纵横比,我怎样才能定义它的高度并让它自动获取它的宽度?

ImageIcon image2 = new ImageIcon(image);

//Check if image size is more than 200

if(!checking){
Image img = image2.getImage() ;
Image newimg = img.getScaledInstance( "What to put here?", 200, java.awt.Image.SCALE_SMOOTH ) ;
image2 = new ImageIcon( newimg );
}

JButton newimage = new JButton(image2);

最佳答案

复制自 Javadocs ,

If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.

所以你可以这样做,

img.getScaledInstance(-1, 200,  java.awt.Image.SCALE_SMOOTH ) ;

关于Java - 如何自动设置 img.getScaledInstance( ); 的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32601155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com