APL/APL2.1/TP15/Image/Image.java
2022-05-24 17:28:29 +02:00

62 lines
2.2 KiB
Java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class Image {
public static void main(String[] args) {
try (BufferedReader r = new BufferedReader(new FileReader("image.xpm"))) {
r.readLine(); r.readLine(); r.readLine(); //On skip les 3 première lignes.
String[] infos = r.readLine().replaceAll("[\" ]", " ").trim().split(" ");
System.out.println(Arrays.toString(infos));
int columns = Integer.parseInt(infos[0]);
int rows = Integer.parseInt(infos[1]);
int colorCount = Integer.parseInt(infos[2]);
int charPerPixel = Integer.parseInt(infos[3]);
char[] paletteId = new char[colorCount];
Color[] palette = new Color[colorCount];
for (int i = 0; i < colorCount; i++) {
String colorInfos = r.readLine().replaceAll("[\"]", "");
paletteId[i] = colorInfos.charAt(0);
System.out.println(colorInfos.substring(4, 11));
palette[i] = Color.decode(colorInfos.substring(4, 11));
}
r.readLine(); //On skip la ligne pixels.
BufferedImage bi = new BufferedImage(columns, rows, BufferedImage.TYPE_3BYTE_BGR);
for (int i = 0; i < rows; i++) {
String row = r.readLine().replaceAll("\"", "");
for (int y = 0; y < columns; y++) {
char a = row.charAt(y);
for (int j = 0; j < paletteId.length; j++) {
if (paletteId[j] == a) {
bi.setRGB(y, i, palette[j].getRGB());
}
}
}
}
JFrame j = new JFrame();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(500 + 50, 500 + 50);
j.setLayout(null);
AwesomeImage a = new AwesomeImage(bi);
a.setLocation(10, 10);
a.setSize(500, 500);
j.add(a);
j.setVisible(true);
} catch (IOException e) {
}
}
}