0
I made a small app to show my problem; whenever the player makes a sequence diagonally, wins, if no one does that, it’s a draw; don’t look at the fact that the second player will never win... I just made an example to show that by winning or tying, I wanted the images to appear a little before they were undone; for example, when someone wins, one image appears and the other one disappears, when the winner’s message appears. Look. Both images are 70 pixels... the code compiles nicely. Look:
package example;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
public class Example extends Application{
private final Button[] BUTTONS = new Button[4];
private boolean JOGADOR_UM = true;
private boolean JOGADOR_DOIS = false;
private static DropShadow DROPSHADOW = new DropShadow();
private final String [][] MATRIZ = new String[2][2];
private final String [] TABELA = new String[2];
private final Image[] IMAGE = {
new Image(Example.class.getResource("0.png").toString()),
new Image(Example.class.getResource("1.png").toString();
};
public Parent createContent(){
StackPane root = new StackPane();
TilePane tilePane = new TilePane();
tilePane.setAlignment(Pos.CENTER);
tilePane.setPrefColumns(2);
for (int i = 0; i<4; i++){
BUTTONS[i] = new Button();
BUTTONS[i].setPrefSize(90, 90);
tilePane.getChildren().add(BUTTONS[i]);
}
BUTTONS[0].setOnAction((ActionEvent) ->{
if(JOGADOR_UM== true){
if(BUTTONS[0].getEffect() == null){
BUTTONS[0].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[0].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(0, "X");
}
} else{
if(BUTTONS[0].getEffect() == null){
BUTTONS[0].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[0].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(0, "O");
}
}
});
BUTTONS[1].setOnAction((ActionEvent) ->{
if(JOGADOR_UM == true){
if(BUTTONS[1].getEffect() == null){
BUTTONS[1].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[1].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(1, "X");
}
} else{
if(BUTTONS[1].getEffect() == null){
BUTTONS[1].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[1].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(1, "O");
}
}
});
BUTTONS[2].setOnAction((ActionEvent) ->{
if(JOGADOR_UM == true){
if(BUTTONS[2].getEffect() == null){
BUTTONS[2].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[2].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(2, "X");
}
} else{
if (BUTTONS[2].getEffect() == null){
BUTTONS[2].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[2].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(2, "O");
}
}
});
BUTTONS[3].setOnAction((ActionEvent) ->{
if(JOGADOR_UM == true){
if(BUTTONS[3].getEffect() == null){
BUTTONS[3].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[3].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(3, "X");
}
} else{
if(BUTTONS[3].getEffect() == null){
BUTTONS[3].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[3].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(3, "O");
}
}
});
root.getChildren().add(tilePane);
return root;
}
public void validarJogada(){
if(JOGADOR_UM == true){
JOGADOR_UM = false;
JOGADOR_DOIS = true;
}else{
JOGADOR_UM = true;
JOGADOR_DOIS = false;
}
}
public void registrarJogada(int p, String jogada){
if(p == 0){
MATRIZ[0][0] = jogada;
}else if(p == 1){
MATRIZ[0][1] = jogada;
}
if(p == 2){
MATRIZ[1][0] = jogada;
}else if(p == 3){
MATRIZ[1][1] = jogada;
}
verificarGanhador();
}
public void verificarGanhador(){
TABELA[0] = MATRIZ[0][0] + MATRIZ[1][1];
TABELA[1] = MATRIZ[1][0] + MATRIZ[0][1];
for (String tabela: TABELA) {
switch (tabela) {
case "XX":
ganhador("X");
break;
case "OO":
ganhador("O");
break;
}
}
if (BUTTONS[0].getEffect()!= (null) &&
BUTTONS[1].getEffect() != (null) &&
BUTTONS[2].getEffect() != (null) &&
BUTTONS[3].getEffect() != (null)){
ganhador("XO");
}
}
public void ganhador(String ganhador){
switch(ganhador){
case "X":
JOptionPane.showMessageDialog(null, "Jogador 1 ganhou!");
limparJogo();
break;
case "O":
JOptionPane.showMessageDialog(null, "Jogador 2 ganhou!");
limparJogo();
break;
case "XO":
JOptionPane.showMessageDialog(null, "Empatou");
limparJogo();
break;
}
}
public void limparJogo(){
BUTTONS[0].setGraphic(null);
BUTTONS[1].setGraphic(null);
BUTTONS[2].setGraphic(null);
BUTTONS[3].setGraphic(null);
BUTTONS[0].setEffect(null);
BUTTONS[1].setEffect(null);
BUTTONS[2].setEffect(null);
BUTTONS[3].setEffect(null);
for (String[] m : MATRIZ) {
for (int i = 0; i<MATRIZ.length; i++) {
m[i] = ("");
}
}
JOGADOR_UM = true;
JOGADOR_DOIS = false;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent(), 200, 200));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String [] args){launch(args);}
}
Do you want to display the image that is below, if they are different? la where ta setGraphic Voce can set the image of the local position
– Michel Simões
Yes... I want to show off, but right away, you must "go back down", since you are different.
– Carlos Ximendes
You can demonstrate in code, Michel Simões.
– Carlos Ximendes
Guys, when applied in Swing, this problem does not occur; I think it is typical of Javafx, because I made the change for it. This has happened before. It must be something related to Imageview... will be?
– Carlos Ximendes