1
I would like to resize an image to be shown in my jLabel. Here’s my code:
BufferedImage imagem;
Icon novaImg;
try {
String newPath = "file:///" + aleat(); // carrega imagem aleatória
imagem = ImageIO.read(new URL(newPath));
novaImg = new ImageIcon(imagem);
jLabelPrincipal.setIcon(novaImg);
} catch(IOException exception) {
System.err.println(exception);
}
imagem = (BufferedImage) imagem.getScaledInstance(largura, altura, java.awt.Image.SCALE_SMOOTH);

java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
– Luan R
@user5693, edited the answer and now this' without the casting.
– Mansueli
This new one was really good, exactly the way I wanted it! Thank you!
– Luan R