32 lines
919 B
Java
32 lines
919 B
Java
|
import java.awt.*;
|
||
|
import javax.swing.*;
|
||
|
import java.awt.event.*;
|
||
|
import java.lang.*;
|
||
|
import javax.imageio.*;
|
||
|
import java.io.*;
|
||
|
|
||
|
public class Imag extends JComponent{
|
||
|
private Image image;
|
||
|
|
||
|
public Imag(int width, int height, String path) throws IOException {
|
||
|
try{
|
||
|
image = ImageIO.read(new File(path));
|
||
|
}catch(IOException e){
|
||
|
System.out.println("Erreur d'ouverture");
|
||
|
}
|
||
|
setPreferredSize(new Dimension(width, height));
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
|
||
|
protected void paintComponent(Graphics pinceau) {
|
||
|
// obligatoire : on cree un nouveau pinceau pour pouvoir le modifier plus tard
|
||
|
Graphics pinceau2 = pinceau.create();
|
||
|
if (this.isOpaque()) {
|
||
|
// obligatoire : on repeint toute la surface avec la couleur de fond
|
||
|
pinceau2.setColor(this.getBackground());
|
||
|
pinceau2.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||
|
}
|
||
|
pinceau2.drawImage(this.image, 0, 0, 180, 180, this);
|
||
|
}
|
||
|
}
|