调整图像大小以适合框架 [英] Resizing image to fit frame
问题描述
我的应用程序的一部分是作为教程。为此,我得到了JPanels,它显示了一个以图像为内容的JLabel。虽然当图像大于适合屏幕的图像时,它会切割不适合的图像。我需要做的是调整图像大小,以便适应JLabel给出的空间。也尝试使用JScrollPanes但没有任何区别(虽然我更喜欢调整图像大小)。
A part of my application is working as a tutorial. For that purpose I got JPanels that display a JLabel that has an image as content. Although when the image is larger than what fits the screen it will just cut what doesn't fit. What I need to do is resize the image so that I will fit the space the JLabel is given. Also tried using JScrollPanes but didn't make any difference(although I prefer to resize image).
我尝试使用
Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
但它没有任何区别。
这是当前代码:
private JLabel getTutorialLabel(int type) {
String path = "/Images/Tutorial/test" + type + ".png";
try {
BufferedImage tutorialPic = ImageIO.read(getClass().getResource(path));
// int height = (int) (screenDim.height * 0.9);
// int width = (int) (screenDim.width * 0.9);
// Image scaledImg = myPicture.getScaledInstance(width, height, Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(tutorialPic));
return picLabel;
} catch (IOException ex) {
Logger.getLogger(Tutorial.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
如何正确显示图像?
How I get the image to display properly?
请注意,还有其他组件与JLabel一起显示(JMenuBar和屏幕底部的一些按钮)所以我需要成像填补空格JLabel。
Please note that there are other components displayed along with the JLabel(JMenuBar and some buttons on bottom of screen) so I need to image to fill the space JLabel is given.
推荐答案
编辑:更新方法
您可以尝试此方法在 Java Helper Library 中。
或者,这是方法本身:
/**
* This method resizes the given image using Image.SCALE_SMOOTH.
*
* @param image the image to be resized
* @param width the desired width of the new image. Negative values force the only constraint to be height.
* @param height the desired height of the new image. Negative values force the only constraint to be width.
* @param max if true, sets the width and height as maximum heights and widths, if false, they are minimums.
* @return the resized image.
*/
public static Image resizeImage(Image image, int width, int height, boolean max) {
if (width < 0 && height > 0) {
return resizeImageBy(image, height, false);
} else if (width > 0 && height < 0) {
return resizeImageBy(image, width, true);
} else if (width < 0 && height < 0) {
PrinterHelper.printErr("Setting the image size to (width, height) of: ("
+ width + ", " + height + ") effectively means \"do nothing\"... Returning original image");
return image;
//alternatively you can use System.err.println("");
//or you could just ignore this case
}
int currentHeight = image.getHeight(null);
int currentWidth = image.getWidth(null);
int expectedWidth = (height * currentWidth) / currentHeight;
//Size will be set to the height
//unless the expectedWidth is greater than the width and the constraint is maximum
//or the expectedWidth is less than the width and the constraint is minimum
int size = height;
if (max && expectedWidth > width) {
size = width;
} else if (!max && expectedWidth < width) {
size = width;
}
return resizeImageBy(image, size, (size == width));
}
/**
* Resizes the given image using Image.SCALE_SMOOTH.
*
* @param image the image to be resized
* @param size the size to resize the width/height by (see setWidth)
* @param setWidth whether the size applies to the height or to the width
* @return the resized image
*/
public static Image resizeImageBy(Image image, int size, boolean setWidth) {
if (setWidth) {
return image.getScaledInstance(size, -1, Image.SCALE_SMOOTH);
} else {
return image.getScaledInstance(-1, size, Image.SCALE_SMOOTH);
}
}
这篇关于调整图像大小以适合框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!