56 lines
1.6 KiB
Java
56 lines
1.6 KiB
Java
|
|
||
|
|
||
|
import javax.swing.*;
|
||
|
import java.awt.*;
|
||
|
import java.io.DataInputStream;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.ObjectInputStream;
|
||
|
|
||
|
public class Polygone extends JPanel {
|
||
|
private int[] x;
|
||
|
private int[] y;
|
||
|
|
||
|
public Polygone() {
|
||
|
super();
|
||
|
|
||
|
try {
|
||
|
|
||
|
FileInputStream fis = new FileInputStream("./polygone.bin");
|
||
|
DataInputStream b = new DataInputStream(fis);
|
||
|
|
||
|
x = new int[b.available() / 8];
|
||
|
y = new int[b.available() / 8];
|
||
|
|
||
|
for (int i = 0; i < x.length; i++) {
|
||
|
x[i] = b.readInt() * 2;
|
||
|
y[i] = b.readInt() * 2;
|
||
|
System.out.println(x[i] + ", " + y[i]);
|
||
|
}
|
||
|
|
||
|
b.close();
|
||
|
} catch (IOException e) {
|
||
|
System.out.println(e);
|
||
|
}
|
||
|
|
||
|
this.repaint();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void paintComponent(Graphics g) {
|
||
|
// obligatoire : on crée un nouveau pinceau pour pouvoir le modifier plus tard
|
||
|
Graphics secondPinceau = g.create();
|
||
|
// obligatoire : si le composant n'est pas censé être transparent
|
||
|
if (this.isOpaque()) {
|
||
|
// obligatoire : on repeint toute la surface avec la couleur de fond
|
||
|
secondPinceau.setColor(this.getBackground());
|
||
|
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||
|
}
|
||
|
// maintenant on dessine ce que l'on veut
|
||
|
secondPinceau.setColor(Color.BLACK);
|
||
|
if (x != null) {
|
||
|
secondPinceau.fillPolygon(x, y, x.length);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|