-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJanelaPrincipal.java
301 lines (255 loc) · 11.4 KB
/
JanelaPrincipal.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* Tela do jogo.
* Respons�vel por reagir aos cliques feitos pelo jogador.
*
* @author Alan Moraes <[email protected]>
* @author Leonardo Villeth <[email protected]>
*/
public class JanelaPrincipal extends JFrame {
private Jogo jogo;
private boolean primeiroClique;
private CasaGUI casaClicadaOrigem;
private CasaGUI casaClicadaDestino;
/**
* Responde aos cliques realizados no tabuleiro.
*
* @param casaClicada Casa que o jogador clicou.
*/
public void reagir(CasaGUI casaClicada) {
if (primeiroClique) {
if (casaClicada.possuiPeca()) {
casaClicadaOrigem = casaClicada;
casaClicadaOrigem.destacar();
primeiroClique = false;
}
else {
// clicou em uma posi�?o inv�lida, ent?o n?o faz nada.
JOptionPane.showMessageDialog(this, "Clique em uma peça.");
}
}
else {
casaClicadaDestino = casaClicada;
jogo.moverPeca(casaClicadaOrigem.getPosicaoX(), casaClicadaOrigem.getPosicaoY(),
casaClicadaDestino.getPosicaoX(), casaClicadaDestino.getPosicaoY());
casaClicadaOrigem.atenuar();
primeiroClique = true;
atualizar();
}
}
/**
* Construtor da classe.
*/
public JanelaPrincipal() {
initComponents();
this.primeiroClique = true;
this.casaClicadaOrigem = null;
this.casaClicadaDestino = null;
criarNovoJogo();
// configura action listener para o menu novo
menuNovo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
criarNovoJogo();
}
});
// configura action listener para o menu sair
menuSair.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
super.setLocationRelativeTo(null);
super.setVisible(true);
super.pack();
}
/**
* Cria um novo jogo e atualiza o tabuleiro gr�fico.
*/
private void criarNovoJogo() {
if(!primeiroClique) {
primeiroClique = true;
casaClicadaOrigem.atenuar();
}
jogo = new Jogo();
atualizar();
}
private void atualizar() {
tabuleiroGUI.atualizar(jogo);
}
/**
* 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() {
pnlLinhas = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
pnlColunas = new javax.swing.JPanel();
lbl_a = new javax.swing.JLabel();
lbl_b = new javax.swing.JLabel();
lbl_c = new javax.swing.JLabel();
lbl_d = new javax.swing.JLabel();
lbl_e = new javax.swing.JLabel();
lbl_f = new javax.swing.JLabel();
lbl_g = new javax.swing.JLabel();
lbl_h = new javax.swing.JLabel();
tabuleiroGUI = new TabuleiroGUI(this);
jMenuBar1 = new javax.swing.JMenuBar();
menuArquivo = new javax.swing.JMenu();
menuNovo = new javax.swing.JMenuItem();
jSeparator1 = new javax.swing.JPopupMenu.Separator();
menuSair = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pnlLinhas.setLayout(new java.awt.GridLayout(8, 1));
jLabel3.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("7");
pnlLinhas.add(jLabel3);
jLabel4.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel4.setText("6");
pnlLinhas.add(jLabel4);
jLabel5.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setText("5");
pnlLinhas.add(jLabel5);
jLabel6.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel6.setText("4");
pnlLinhas.add(jLabel6);
jLabel7.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel7.setText("3");
pnlLinhas.add(jLabel7);
jLabel8.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel8.setText("2");
pnlLinhas.add(jLabel8);
jLabel2.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("1");
pnlLinhas.add(jLabel2);
jLabel1.setFont(new java.awt.Font("Abyssinica SIL", 0, 18)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("0");
pnlLinhas.add(jLabel1);
pnlColunas.setLayout(new java.awt.GridLayout(1, 8));
lbl_a.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_a.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_a.setText("0");
pnlColunas.add(lbl_a);
lbl_b.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_b.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_b.setText("1");
pnlColunas.add(lbl_b);
lbl_c.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_c.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_c.setText("2");
pnlColunas.add(lbl_c);
lbl_d.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_d.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_d.setText("3");
pnlColunas.add(lbl_d);
lbl_e.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_e.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_e.setText("4");
pnlColunas.add(lbl_e);
lbl_f.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_f.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_f.setText("5");
pnlColunas.add(lbl_f);
lbl_g.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_g.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_g.setText("6");
pnlColunas.add(lbl_g);
lbl_h.setFont(new java.awt.Font("Arimo", 0, 18)); // NOI18N
lbl_h.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lbl_h.setText("7");
pnlColunas.add(lbl_h);
menuArquivo.setText("Jogo");
menuNovo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
menuNovo.setText("Novo");
menuArquivo.add(menuNovo);
menuArquivo.add(jSeparator1);
menuSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
menuSair.setText("Sair");
menuArquivo.add(menuSair);
jMenuBar1.add(menuArquivo);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(pnlLinhas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(pnlColunas, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
.addComponent(tabuleiroGUI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(pnlLinhas, javax.swing.GroupLayout.PREFERRED_SIZE, 576, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tabuleiroGUI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pnlColunas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JanelaPrincipal().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JLabel lbl_a;
private javax.swing.JLabel lbl_b;
private javax.swing.JLabel lbl_c;
private javax.swing.JLabel lbl_d;
private javax.swing.JLabel lbl_e;
private javax.swing.JLabel lbl_f;
private javax.swing.JLabel lbl_g;
private javax.swing.JLabel lbl_h;
private javax.swing.JMenu menuArquivo;
private javax.swing.JMenuItem menuNovo;
private javax.swing.JMenuItem menuSair;
private javax.swing.JPanel pnlColunas;
private javax.swing.JPanel pnlLinhas;
private TabuleiroGUI tabuleiroGUI;
// End of variables declaration//GEN-END:variables
}