1

私のアプリケーションの一部はチュートリアルとして機能しています。その目的のために、コンテンツとして画像を持つ JLabel を表示する JPanels を取得しました。ただし、画像が画面に収まるサイズよりも大きい場合は、収まらない部分が切り取られます。私がする必要があるのは、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;
    }

画像を正しく表示するにはどうすればよいですか?

JLabel (JMenuBar と画面下部のいくつかのボタン) と共に表示される他のコンポーネントがあることに注意してください。そのため、JLabel が指定されたスペースを埋めるために画像を作成する必要があります。

4

1 に答える 1

2

編集:メソッドを更新しました

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);
  }
}
于 2012-04-25T00:14:47.003 に答える