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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
public class testImageLabel extends JFrame {
private JPanel panel = new JPanel();
private JLabel imageLabel;
private ImageIcon icon = new ImageIcon("Image.jpg");
public testImageLabel() {
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test Image Label");
this.setSize(400, 300);
Image imageZoom = scaleImage(icon.getImage(), 100);
Icon scaledIcon = new ImageIcon(imageZoom);
imageLabel = new JLabel("Label", scaledIcon, 10);
panel.add(imageLabel, BorderLayout.CENTER);
this.panel.setBackground(Color.white);
this.getContentPane().add(panel);
this.setVisible(true);
}
//Méthode pour redimensionner une image
public static Image scaleImage(Image source, int width, int height) {
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D)img.getGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, width, height, null);
g.dispose();
return img;
}
//Méthode de calcul (en pixels) des paramètres widht et height de la méthode précédente
public static Image scaleImage(Image source, int size) {
int width = source.getWidth(null);
int height = source.getHeight(null);
double f = 0;
if (width < height) { // portrait
f = (double)height / (double)width;
width = (int)(size / f);
height = size;
} else { //paysage
f = (double)width / (double)height;
width = size;
height = (int)(size / f);
}
return scaleImage(source, width, height);
}
public static void main(String[] args) {
testImageLabel test = new testImageLabel();
}
} |
Partager