画像のサイズを 50 * 50 ピクセルに変更しようとしています。データベースに保存されているパスから画像を取得しています。画像の取得と表示に問題はありません。どの時点で画像のサイズを変更する必要があるのか 疑問に思っています。画像をバッファリングされた画像として取得するとき、または単にアイコンのサイズを変更しようとするときでしょうか?
while (rs.next()) {
i = 1;
imagePath = rs.getString("path");
System.out.println(imagePath + "\n");
System.out.println("TESTING - READING IMAGE");
System.out.println(i);
myImages[i] = ImageIO.read(new File(imagePath));
**resize(myImages[i]);**
imglab[i] = new JLabel(new ImageIcon(myImages[i]));
System.out.println(i);
imgPanel[i]= new JPanel();
imgPanel[i].add(imglab[i]);
loadcard.add(imgPanel[i], ""+i);
i++;
上記のコードは、画像を取得して ImageIcon に割り当て、次に JLabel に割り当てています。以下のサイズ変更方法を使用して、バッファリングされた画像のサイズを変更しようとしました。皆さん、これが私にとってうまくいかない理由を教えていただけますか? エラーは発生せず、画像は元のサイズのままです。
public static BufferedImage resize(BufferedImage img) {
int w = img.getWidth();
int h = img.getHeight();
int newH = 50;
int newW = 50;
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
System.out.println("Is this getting here at all " + dimg);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
}