-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTabuleiroGUI.java
158 lines (144 loc) · 5.34 KB
/
TabuleiroGUI.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.awt.Color;
import javax.swing.JPanel;
/**
* Interface Grafica do Tabuleiro do jogo.
*
* @author Alan Moraes <alan@ci.ufpb.br>
* @author Leonardo Villeth <lvilleth@cc.ci.ufpb.br>
*/
public class TabuleiroGUI extends JPanel {
private JanelaPrincipal janela;
private CasaGUI[][] casas;
/**
* Creates new form Tabuleiro
*/
public TabuleiroGUI() {
// Construtor sem par�metros requerido pela especifica�?o JavaBeans.
}
public TabuleiroGUI(JanelaPrincipal janela) {
this.janela = janela;
initComponents();
criarCasas();
}
/**
* Preenche o tabuleiro com 64 casas
*/
private void criarCasas() {
casas = new CasaGUI[8][8];
// De cima para baixo
for (int y = 7; y >= 0; y--) {
// Da esquerda para a direita
for (int x = 0; x < 8; x++) {
Color cor = calcularCor(x, y);
CasaGUI casa = new CasaGUI(x, y, cor, this);
casas[x][y] = casa;
add(casa);
}
}
}
private Color calcularCor(int x, int y) {
// linha par
if (x % 2 == 0) {
// coluna �mpar
if (y % 2 == 0) {
return CasaGUI.COR_ESCURA;
}
// coluna �mpar
else {
return CasaGUI.COR_CLARA;
}
}
// linha �mpar
else {
// coluna par
if (y % 2 == 0) {
return CasaGUI.COR_CLARA;
}
// coluna �mpar
else {
return CasaGUI.COR_ESCURA;
}
}
// codigo acima em uma linha
// return (i%2 + j%2)%2 == 0 ? CasaGUI.COR_ESCURA : CasaGUI.COR_CLARA;
}
public void atualizar(Jogo jogo) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
CasaGUI casaGUI = casas[x][y];
Tabuleiro tabuleiro = jogo.getTabuleiro();
Casa casa = tabuleiro.getCasa(x, y);
if (casa.possuiPeca()) {
Peca peca = casa.getPeca();
/* utilizar da propriedade de fallthrough do switch case +
* switch case encadeados (Aurenívia)
*/
switch (peca.getCor()){
case BRANCO:
switch(peca.getTipo()){
case TORRE:
casaGUI.desenharTorreBranca();
break;
case CAVALO:
casaGUI.desenharCavaloBranco();
break;
case BISPO:
casaGUI.desenharBispoBranco();
break;
case RAINHA:
casaGUI.desenharRainhaBranca();
break;
case REI:
casaGUI.desenharReiBranco();
break;
case PEAO:
casaGUI.desenharPeaoBranco();
break;
}
break;
case PRETO:
switch(peca.getTipo()){
case TORRE:
casaGUI.desenharTorrePreta();
break;
case CAVALO:
casaGUI.desenharCavaloPreto();
break;
case BISPO:
casaGUI.desenharBispoPreto();
break;
case RAINHA:
casaGUI.desenharRainhaPreta();
break;
case REI:
casaGUI.desenharReiPreto();
break;
case PEAO:
casaGUI.desenharPeaoPreto();
break;
}
break;
}
}
else {
casaGUI.apagarPeca();
}
}
}
}
public JanelaPrincipal getJanela() {
return janela;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setLayout(new java.awt.GridLayout(8, 8));
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}