Galera eu estou tentando usar o GridBagLayout mas estou com um probelma, as celulas nao tem um numero fixo, por exemplo a celula (0,0) é na verdade (0,2) estou mandando o código pra esclarecer minha duvida
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class InterfaceGrafica extends JFrame {
protected JPanel painelPrincipal;
protected Vector<JPanel> casas;
protected Color cor;
protected GridBagConstraints cons, ultimoClick;
public InterfaceGrafica() {
casas = new Vector<JPanel>();
painelPrincipal = new JPanel();
painelPrincipal.setLayout(new GridBagLayout());
cons = new GridBagConstraints();
ultimoClick = new GridBagConstraints();
cons.weightx = 1;
cons.weighty = 1;
cons.fill = GridBagConstraints.BOTH;
for (int i = 0; i < 64; i++) {
casas.add(new JPanel());
casas.get(i).addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {
cor = e.getComponent().getBackground();
e.getComponent().setBackground(Color.YELLOW);
}
public void mouseExited(MouseEvent e) {
e.getComponent().setBackground(cor);
}
public void mouseClicked(MouseEvent e) {
ultimoClick.gridx = e.getComponent().getX();
ultimoClick.gridy = e.getComponent().getY();
System.out.println("x: " + ultimoClick.gridx + "\ny: "
+ ultimoClick.gridy);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
if (((((i < 8) | ((i > 16) & (i < 24)) | ((i > 32) & (i < 40)) | ((i > 48) & (i < 56))) & (i % 2 == 1)))
| (((((i > 7) & (i < 15)) | ((i > 23) & (i < 31))
| ((i > 39) & (i < 47)) | (i > 55)) & (i % 2 == 0)))) {
casas.get(i).setBackground(Color.BLACK);
} else {
casas.get(i).setBackground(Color.WHITE);
}
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cons.gridx = j;
cons.gridy = i;
painelPrincipal.add(casas.get(i * 8 + j), cons);
}
}
add(painelPrincipal);
setSize(400, 400);
setLocation(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
InterfaceGrafica jogo = new InterfaceGrafica();
}
}
[code] public void mouseClicked(MouseEvent e) {
ultimoClick.gridx = e.getComponent().getX();
ultimoClick.gridy = e.getComponent().getY();
System.out.println("x: " + ultimoClick.gridx + "\ny: "
+ ultimoClick.gridy);
}
[/code]
Ao meu ver o problema está nessa porção de código, bom os métodos getX e getY, até onde observei tem muito haver com o tamanho da janela que você está usando… dei uns resizes no JFrame e recebi resultados diferentes para a mesma casa clicada…
Bom a minha sugestão seria fazer uma nova classe que seja filha de JPanel…
public class Casa extends javax.swing.JPanel{
private int x, y;
public Casa() {
}
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
public int getPosicaoX(){
return this.x;
}
public int getPosicaoY(){
return this.y;
}
}
Utilizando Essa nova classe na sua classe main…
temos um código que funciona dentro do que você espera, pelo menos eu acho…
[code]import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class InterfaceGrafica extends JFrame {
protected JPanel painelPrincipal;
protected Vector<Casa> casas;
protected Color cor;
protected GridBagConstraints cons, ultimoClick;
public InterfaceGrafica() {
casas = new Vector<Casa>();
painelPrincipal = new JPanel();
painelPrincipal.setLayout(new GridBagLayout());
cons = new GridBagConstraints();
ultimoClick = new GridBagConstraints();
cons.weightx = 1;
cons.weighty = 1;
cons.fill = GridBagConstraints.BOTH;
for (int i = 0; i < 64; i++) {
casas.add(new Casa());
casas.get(i).addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {
cor = e.getComponent().getBackground();
e.getComponent().setBackground(Color.YELLOW);
}
public void mouseExited(MouseEvent e) {
e.getComponent().setBackground(cor);
}
public void mouseClicked(MouseEvent e) {
Casa casa = (Casa) e.getComponent();
ultimoClick.gridx = casa.getPosicaoX();
ultimoClick.gridy = casa.getPosicaoY();
System.out.println("x: " + ultimoClick.gridx + "\ny: " + ultimoClick.gridy);
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
if (((((i < 8) | ((i > 16) & (i < 24)) | ((i > 32) & (i < 40)) | ((i > 48) & (i < 56))) & (i % 2 == 1))) | (((((i > 7) & (i < 15)) | ((i > 23) & (i < 31)) | ((i > 39) & (i < 47)) | (i > 55)) & (i % 2 == 0)))) {
casas.get(i).setBackground(Color.BLACK);
} else {
casas.get(i).setBackground(Color.WHITE);
}
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cons.gridx = j;
cons.gridy = i;
painelPrincipal.add(casas.get(i * 8 + j), cons);
casas.get(i * 8 + j).setX(j);
casas.get(i * 8 + j).setY(i);
}
}
add(painelPrincipal);
setSize(400, 400);
setLocation(300, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
InterfaceGrafica jogo = new InterfaceGrafica();
}
}[/code]
nao deu certo isso nao, mas a ideia de criar uma nova classe deu. VLW