Image problem

Asked

Viewed 177 times

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

  • Yes... I want to show off, but right away, you must "go back down", since you are different.

  • You can demonstrate in code, Michel Simões.

  • 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?

2 answers

0

Based on this code posted, it should work like this:

buttons[PrimeiroClick].setGraphic((new ImageView(imgs[Aleatorio[PrimeiroClick]])); 
buttons[SegundoClick].setGraphic((new ImageView(imgs[Aleatorio[SegundoClick]])); 

If it’s not, try posting + of the code.

  • Dude, I don’t get it... I put it, but nothing happened; I edited the code. Take a look at it.

  • 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?

  • Implementation and default declaration, should be.

  • I played an old woman who had the same problem; I hope someone helps me. In Swing, or using numbers in Javafx, instead of images, it works.

0


After much research, I understood this problem. In Swing, and using Joptiopane, there is no problem; the images appear and are only undone when the message is closed. In Javafx, using Joptionpane, there is the problem of undoing the images before the message appears. But if we use Javafx’s own messages, it’s all right.

Would look like this:

`public void winner(String winner){

    switch(ganhador){
        case "X":

            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setContentText("Jogador 1 ganhou!");
            alert.showAndWait();
            limparJogo();

            break;

        case "O":

            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setContentText("Jogador 2 ganhou!");
            alert.showAndWait();
            limparJogo();


            break;

        case "XO":

            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setContentText("Empatou!");
            alert.showAndWait();
            limparJogo();

            break;
    }`

Browser other questions tagged

You are not signed in. Login or sign up in order to post.