Files
DEV/DEV2.1/Flux_octet/image/Imagee.java

40 lines
1.2 KiB
Java
Raw Normal View History

2024-03-25 14:56:28 +01:00
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class Imagee extends JComponent {
private Image image;
public Imagee(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);
byte[] read_buffer = new byte[width * height * 3];
input.read(read_buffer);
int[] int_buffer = new int[read_buffer.length];
for (int i = 0;i < read_buffer.length ; i++)
int_buffer[i] = read_buffer[i] & 0xFF;
img.getRaster().setPixels(0,0,width,height,int_buffer);
return img;
}
@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());
}
secondPinceau.setColor(this.getForeground());
secondPinceau.drawImage(image, 0 ,0 , this);
}
}