-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAffichage1.java
133 lines (107 loc) · 4.68 KB
/
Affichage1.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
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.*;
/**Gestionnaire d'affichage du jeu du Snake issu du
* gestionnaire d'affichage pour la fourmi de Langton (issu de l'affichage du "Jeu de la vie")
* @author Jean-Francois TREGOUET - modification: Thoniel Solal, Kabbadj Ghali, Alban PRATS
*/
public class Affichage1 extends JFrame{
public static Affichage1 world = null;
private PanneauGrille pg;
public Affichage1(Plateau p,String name) {
super(name);
pg = new PanneauGrille(p);
setContentPane(pg);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
/**Méthode permettant de fermer l'affichage.
*/
public void fermer(){
setVisible(false);
}
/**
* Affiche un plateau.
* @param p le plateau à afficher
*/
public static void afficherplateau(Plateau p) {
world.pg.p = p;
world.repaint();
}
/**
* Calcule la résolution la plus appropriée à la taille du plateau de
* façon à ce que la fenêtre occupe 95% de la hauteur ou de la
* largeur de la zone utile de l'écran.
* @param plateau le plateau à afficher
*/
private static int calcRes(int[][] plateau) {
final double p = 0.45; // pourçentage de la zone utile à occuper
int resC;
Rectangle bounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); // Taille zone utile
resC = Math.min((int)(bounds.height*p)/plateau.length,(int)(bounds.width*p)/plateau[0].length);
resC = Math.max(1,resC); // valeur plancher de 1
return resC;
}
class PanneauGrille extends JPanel {
private int res;
private BufferedImage worldImage;
public Plateau p;
public PanneauGrille(Plateau monP) {
p = monP;
res = Affichage1.calcRes(p.plateau);
worldImage = new BufferedImage(res*p.plateau[0].length,res*p.plateau.length,BufferedImage.TYPE_INT_RGB);
setPreferredSize(new Dimension(res*p.plateau[0].length,res*p.plateau.length));
}
private void dessineplateau(Graphics g) {
//Création couleurs
Color tete = new Color (10, 121, 12);
Color monVert = new Color (65, 165, 39);
Color monRouge = new Color (218, 21, 21);
Color monBleu = new Color (1, 68, 240);
int nbL = p.plateau.length;
int nbC = p.plateau[0].length;
// couleur de fond
g.setColor(Color.WHITE);
g.fillRect(0,0,res*nbC,res*nbL);
// couleurs
g.setColor(monRouge);
for (int i = 0; i < nbL; i++)
for (int j = 0; j < nbC; j++)
if (p.plateau[i][j]==3) //couleur de la pomme
g.fillRect(res*j,res*i,res,res);
g.setColor(Color.WHITE);
for (int i = 0; i < nbL; i++)
for (int j = 0; j < nbC; j++)
if (p.plateau[i][j]==2) // couleur du fond
g.fillRect(res*j,res*i,res,res);
g.setColor(Color.BLACK);
for (int i = 0; i < nbL; i++)
for (int j = 0; j < nbC; j++)
if (p.plateau[i][j]==4) // couleur des obstacles
g.fillRect(res*j,res*i,res,res);
g.setColor(monVert);
for (int i = 0; i < nbL; i++)
for (int j = 0; j < nbC; j++)
if (p.plateau[i][j]==1) // couleur de la tête
g.fillRect(res*j,res*i,res,res);
g.setColor(monBleu);
for (int i = 0; i < nbL; i++)
for (int j = 0; j < nbC; j++)
if (p.plateau[i][j]==0) // couleur des powers up
g.fillRect(res*j,res*i,res,res);
//g.setColor(Color.YELLOW);
//for (int i = 0; i < nbL; i++)
// for (int j = 0; j < nbC; j++)
// if (p.plateau[i][j]==8) // couleur des zones interdites
// g.fillRect(res*j,res*i,res,res);
}
public void paint(Graphics g) {
Graphics gw = worldImage.getGraphics();
dessineplateau(gw);
g.drawImage(worldImage,0,0,null);
}
}
}