DEV/DEV2.1/TP9:FluxOctets/Imag.java

42 lines
1.3 KiB
Java
Raw Normal View History

2023-04-27 11:27:11 +02:00
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);
2023-05-09 11:26:45 +02:00
BufferedInputStream input2 = new BufferedInputStream(input);
2023-04-27 11:27:11 +02:00
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());
}
2023-05-09 11:26:45 +02:00
pinceau2.drawImage(this.image, 0, 0, this);
2023-04-27 11:27:11 +02:00
}
}