java - 调整图像大小以适合框架

标签 java image swing jlabel bufferedimage

我的应用程序的一部分用作教程。为此,我使用 JPanels 显示 JLabel,其中包含图像作为内容。尽管当图像大于适合屏幕的图像时,它只会剪切不适合的图像。我需要做的是调整图像大小,以便适合 JLabel 指定的空间。还尝试使用 JScrollPanes 但没有任何区别(尽管我更喜欢调整图像大小)。

我尝试使用

获取图像的缩放实例
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 空间。

最佳答案

编辑:更新了方法

你可以尝试this methodJava 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);
  }
}

关于java - 调整图像大小以适合框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10306335/

相关文章:

java - 我如何确保接口(interface)实现扩展特定类?

java - ImagePanel 未显示在我的带有 netbeans 的 jar 文件中

Java : Get Data from Nested HashMap

Java 观察者模式不通知

javascript - 如何使用 mongoose.js 驱动程序和 express.js 在 mongodb 中保存和查询图像等二进制数据?

php - 获取多个图像以适应未知图像尺寸的浏览器尺寸

java - 为什么我没有达到我的主元排序算法中的基本情况?

java - Android CalendarView 不显示日期

image - 编写颜色配置文件以跨多个扫描仪标准化颜色

java - 如何在不引发异常的情况下强制在 Activity 的 JTextField 中设置文本?