Olá camaradas do GUJ.
Estou desenvolvendo um jogo de damas em java, já criei a parte lógica(validação de jogada) do jogo, agora estou desenvolvendo a parte gráfica.
Criei um frame que implementa um actionListener;
Criei uma matriz de botões que será meu tabuleiro, adicionei em um JPanel;
Adicionei as peças(png) no tabuleiro;
public class GUI extends JFrame implements ActionListener{
JPanel painel;
MyJToggleButton[][] tabBotao;
ImageIcon imgCara = new ImageIcon("imgPecas/cara.png");
ImageIcon imgCoroa = new ImageIcon("imgPecas/coroa.png");
public GUI(){
painel = new JPanel();
tabBotao = new MyJToggleButton[8][8];
painel.setLayout(null);
int x=3;
int y=3;
int c=0;
int l=0;
boolean tf = false;
//criacao do tabuleiro
while(c<8){
while(l<8){
tabBotao[l][c] = new MyJToggleButton();
tabBotao[l][c].setBounds(x, y, 70, 70);
tabBotao[l][c].addActionListener(this);
if(tf == true){
tabBotao[l][c].setBackground(Color.lightGray);
tf= false;
}else{
tabBotao[l][c].setBackground(Color.white);
tf= true;
}
painel.add(tabBotao[l][c]);
y+=70;
l++;
}
c++; l=0;
x+=70; y=3;
if(tf == true) tf= false;
else tf= true;
}
//adicionando as pecas ao tabuleiro
l = 0; // linha
c = 1; // coluna
while (l <= 2) {
while (c <= 7) {
tabBotao[l][c].setIcon(imgCara);
c += 2;
}
l += 1;
if (c % 2 == 0)// se c = par, c recebe 1;
c = 1;
else
// senao, c recebe 0;
c = 0;
}
l = 5; // linha
c = 0; // coluna
while (l <= 7) {
while (c <= 7) {
tabBotao[l][c].setIcon(imgCoroa);
c += 2;
}
l += 1;
if (c % 2 == 0)// se c = par, c recebe 1;
c = 1;
else
// senao, c recebe 0;
c = 0;
}
//configuracoes da janela
this.pack();
this.getContentPane().add(painel);
this.setTitle("Jogo de Damas");
this.setResizable(false); // impede que a janela seja redimensionael
this.setSize(572, 594);
Dimension tamanhoTela = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((tamanhoTela.width - this.getWidth()) / 2,
(tamanhoTela.height - this.getHeight()) / 2);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//como lidar com os eventos para mudança de posição das peças
}
}