-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAffichage.java
137 lines (126 loc) · 4.31 KB
/
Affichage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
/**
* Gestionnaire d'affichage pour la fourmi de Langton (issue de l'affichage du "Jeu de la vie")
* @author Jean-Francois TREGOUET
*/
public class Affichage extends JFrame {
private static Affichage world = null;
private PanneauGrille pg;
private static Affichage worlds[] = null;
private Affichage(Plateau p) {
super("Fourmi de Langton");
pg = new PanneauGrille(p);
setContentPane(pg);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
Affichage(Plateau p, int id) {
super("Fourmi de Langton #"+id);
pg = new PanneauGrille(p);
setContentPane(pg);
pack();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
}
/**
* Affiche un monde.
* @param p le monde à afficher
*/
public static void afficherPlateau(Plateau p) {
if (world==null)
world = new Affichage(p);
world.pg.p = p;
world.repaint();
}
public static void afficherPlateaux(Plateau[] p) {
if(worlds == null) {
worlds = new Affichage[p.length];
for (int i = 0; i < worlds.length; i++) {
worlds[i] = new Affichage(p[i], i);
}
}
for(int i = 0; i < worlds.length; i++) {
worlds[i].pg.p = p[i];
worlds[i].repaint();
}
}
public static void afficherSimulations() {
List<Simulation> sims = Simulation.simulations;
for (Simulation sim : sims) {
if (sim.isActive()) {
sim.getAffichage().pg.p = sim.getPlateau();
sim.getAffichage().repaint();
}
}
}
/**
* Calcul la resolution la plus appropriee a la taille du monde de
* facon a ce que la fenetre occupe 80% de la hauteur ou de la
* largeur de la zone utile de l'ecran
* de l'ecran.
* @param monde le monde à afficher
*/
private static int calcRes(boolean[][] monde) {
final double p = .9; // pourcentage de la zone utile a occuper
int resC;
Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); // Taille zone utile
resC = Math.min((int)(bounds.height*p)/monde.length,(int)(bounds.width*p)/monde[0].length);
resC = Math.max(1,resC); // valeur planche de 1
return resC;
}
class PanneauGrille extends JPanel {
private int res;
private boolean[][] monde;
private BufferedImage worldImage;
public Plateau p;
public PanneauGrille(Plateau monP) {
p = monP;
monde = p.getEtat();
res = Affichage.calcRes(monde);
worldImage = new BufferedImage(res*p.getWidth(),res*p.getHeight(),BufferedImage.TYPE_INT_RGB);
setPreferredSize(new Dimension(res * p.getWidth(), res * p.getHeight()));
}
private void dessineMonde(Graphics g) {
int nbL = p.getHeight();
int nbC = p.getWidth();
// couleur de fond
g.setColor(Color.BLACK);
g.fillRect(0,0,res*nbC,res*nbL);
// cellules
g.setColor(Color.BLACK);
for (int i = 0; i < nbL; i++)
for (int j = 0; j < nbC; j++)
if (monde[i][j]){
Color c = new Color(p.getColors()[i][j]);
g.setColor(c.brighter());
g.fillRect(res*j,res*i,res,res);
if(Math.random() < p.getDecayRate()) {
float b = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null)[2];
p.getColors()[i][j] = c.darker().getRGB();
if(b < p.getBrightnessThreshold()) p.getEtat()[i][j] = false;
}
}
// fourmi
for (Fourmi f : p.getFourmis()) {
if(f == null) continue;
g.setColor(f.getColor());
g.fillRect(f.getColonne()*res,f.getLigne()*res,res,res);
}
}
public void paint(Graphics g) {
Graphics gw = worldImage.getGraphics();
dessineMonde(gw);
g.drawImage(worldImage,0,0,null);
}
}
public static Affichage[] getWorlds() { return worlds; }
}