41 lines
1.2 KiB
Java
41 lines
1.2 KiB
Java
|
import java.awt.*;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import javax.swing.*;
|
||
|
import java.io.*;
|
||
|
|
||
|
public class Imag extends JComponent{
|
||
|
private Image image;
|
||
|
|
||
|
public Imag(int width, int height, String path) throws IOException {
|
||
|
image = readFromFile(width, height, path);
|
||
|
setPreferredSize(new Dimension(width, height));
|
||
|
}
|
||
|
|
||
|
private Image readFromFile(int width, int height, String path) throws IOException {
|
||
|
FileInputStream input = new FileInputStream(path);
|
||
|
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
|
||
|
for (int i = 0; i < height; i++){
|
||
|
for (int j = 0; j < width; j++){
|
||
|
int r = input.read();
|
||
|
int g = input.read();
|
||
|
int b = input.read();
|
||
|
int rgb = (r << 16 | (g << 8) | (b));
|
||
|
img.setRGB(j, i, rgb);
|
||
|
}
|
||
|
}
|
||
|
return img;
|
||
|
}
|
||
|
|
||
|
@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, 768, 1024, this);
|
||
|
}
|
||
|
}
|