Jump to content
Sign in to follow this  
ricardoneves93

TicTacToe

Recommended Posts

ricardoneves93

Boa noite estou a tentar fazer o jogo do galo em java com 9 botões, mas surgiu o seguinte problema, quando carrego num botão quero colocá-lo "non clickable" faço isso com o método setEnabled(), ele realmente fica de outra cor, mas eu tenho a mostrar um png em cada botao sempre que o jogador faz uma jogada, mas mesmo de depois de definir setEnabled(false), o botão continua clickable e o png que estava lá desenhado volta a mudar!

Alguma sugestão?

Share this post


Link to post
Share on other sites
eatg75

Viva! Em primeiro lugar estas a referir a um jogo para o Android ou para o desktop utilizando o Swing? Se for para o Android acho que a operacao setClickAble() ou algo do genero serve, para o Swing procura por funcao equivalente ao setClickAble().


Victarion seized the dusky woman by the wrist and pulled her to him.

Victarion - She will do it. Go pray to your red god. Light your fire, and tell me what you see.

Moqorro's dark eyes seemed to shine.

Moqorro - I see dragons.

Share this post


Link to post
Share on other sites
rezanov

Que tipo de evento está associado ao botão ? MouseListener, ActionListener ?

Share this post


Link to post
Share on other sites
ricardoneves93

MouseListener:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Start extends JFrame implements ActionListener{
JButton b00, b01, b02, b10, b11, b12, b20, b21, b22;
ImageIcon X = new ImageIcon("resources/X.png");
ImageIcon O = new ImageIcon("resources/O.png");
boolean turn = true; //starts x
String winner;
public Start(){
 super("Jogo do Galo");
 setLayout(new GridLayout(3,3));
 b00 = new JButton("");
 b00.addActionListener(this);
 add(b00);
 b01 = new JButton("");
 b01.addActionListener(this);
 add(b01);
 b02 = new JButton("");
 b02.addActionListener(this);
 add(b02);
 b10 = new JButton("");
 b10.addActionListener(this);
 add(b10);
 b11 = new JButton("");
 b11.addActionListener(this);
 add(b11);
 b12 = new JButton("");
 b12.addActionListener(this);
 add(b12);
 b20 = new JButton("");
 b20.addActionListener(this);
 add(b20);
 b21 = new JButton("");
 b21.addActionListener(this);
 add(b21);
 b22 = new JButton("");
 b22.addActionListener(this);
 add(b22);


}
public void actionPerformed(ActionEvent e){

 if(e.getSource() == b00){
  b00.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 b00.setEnabled(false);
 if(turn == true)
 {
  b00.setIcon(X);
  turn = false;
 }
 else
 {
  b00.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });
 }

 if(e.getSource() == b01){
  b01.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b01.setIcon(X);
  turn = false;
 }
 else
 {
  b01.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });
 }

 if(e.getSource() == b02){
  b02.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b02.setIcon(X);
  turn = false;
 }
 else
 {
  b02.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });


 }


 if(e.getSource() == b10){
  b10.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b10.setIcon(X);
  turn = false;
 }
 else
 {
  b10.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });


 }



 if(e.getSource() == b11){
  b11.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b11.setIcon(X);
  turn = false;
 }
 else
 {
  b11.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });


 }



 if(e.getSource() == b12){
  b12.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b12.setIcon(X);
  turn = false;
 }
 else
 {
  b12.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });


 }


 if(e.getSource() == b20){
  b20.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b20.setIcon(X);
  turn = false;
 }
 else
 {
  b20.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });


 }


 if(e.getSource() == b21){
  b21.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b21.setIcon(X);
  turn = false;
 }
 else
 {
  b21.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });


 }


 if(e.getSource() == b22){
  b22.addMouseListener(new MouseAdapter()
  {
   public void mouseClicked(MouseEvent e)
   {
 System.out.println(turn);
 if(turn == true)
 {
  b22.setIcon(X);
  turn = false;
 }
 else
 {
  b22.setIcon(O);
  turn = true;
 }
 Winner();
   }
  });


 }

}
public void Winner(){
 if(b00.getIcon() == X && b01.getIcon() == X && b02.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b00.getIcon() == O && b01.getIcon() == O && b02.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }
 if(b10.getIcon() == X && b11.getIcon() == X && b12.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b10.getIcon() == O && b11.getIcon() == O && b12.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }
 if(b20.getIcon() == X && b21.getIcon() == X && b22.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b20.getIcon() == O && b21.getIcon() == O && b22.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }
 if(b00.getIcon() == X && b10.getIcon() == X && b20.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b00.getIcon() == O && b10.getIcon() == O && b20.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }
 if(b01.getIcon() == X && b11.getIcon() == X && b21.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b01.getIcon() == O && b11.getIcon() == O && b21.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }
 if(b02.getIcon() == X && b12.getIcon() == X && b22.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b02.getIcon() == O && b12.getIcon() == O && b22.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }
 if(b00.getIcon() == X && b11.getIcon() == X && b22.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b00.getIcon() == O && b11.getIcon() == O && b22.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }
 if(b02.getIcon() == X && b11.getIcon() == X && b20.getIcon() == X){
  winner = "X";
  System.out.println(winner);
 }
 if(b02.getIcon() == O && b11.getIcon() == O && b20.getIcon() == O){
  winner = "O";
  System.out.println(winner);
 }

}
}

Share this post


Link to post
Share on other sites
rezanov

Podes fazer como antes, mas vais ter sempre que descobrir qual o button que queres com e.getSource(), como tens feito .... mais botoes, mais comparações.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.