BUT2/DEV/DEV2.1/TP06_Dessin/Q3_Acceuil/AfficheImage.java~

28 lines
835 B
Java
Raw Normal View History

2023-10-23 13:23:36 +02:00
import javax.swing.*;
import java.awt.*;
public class AfficheImage extends JComponent {
private Image fond;
public AfficheImage(String cheminImage) {
super();
this.fond = Toolkit.getDefaultToolkit().getImage(cheminImage);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(288, 215);
}
@Override
protected void paintComponent(Graphics pinceau) {
Graphics secondPinceau = pinceau.create();
if (this.isOpaque()) {
secondPinceau.setColor(this.getBackground());
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
}
int x = (this.getWidth() - fond.getWidth(this)) / 2;
int y = (this.getHeight() - fond.getHeight(this)) / 2;
secondPinceau.drawImage(this.fond, x, y, this);
}
}