1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public void setPhoto(String photo) {
txtPhoto.setText(photo);
if(txtPhoto.getText().isEmpty()==false){
ImageIcon icone = new ImageIcon(txtPhoto.getText());
img = icone.getImage();
Image phot=scale(img,panel.getWidth(),panel.getHeight());
BufferedImage bufImage = new BufferedImage(phot.getWidth(null), phot.getHeight(null), BufferedImage.TYPE_INT_RGB);
bufImage.getGraphics().drawImage(phot, 0, 0, null);
ImageIcon icone2 = new ImageIcon(bufImage);
JLabel ima = new JLabel(icone2);
panel.add(ima);
}
}
public static Image scale(Image source, int width, int height) {
/* On crée une nouvelle image aux bonnes dimensions. */
BufferedImage buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
/* On dessine sur le Graphics de l'image bufferisée. */
Graphics2D g = buf.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(source, 0, 0, width, height, null);
g.dispose();
/* On retourne l'image bufferisée, qui est une image. */
return buf;
} |
Partager