У меня возник вопрос по поводу JavaFX и возможности проверить графику.поэтому в моей программе у меня установлена графика для моих кнопок, когда пользователь нажимает ее.
Что мне нужно выяснить, это то, что мне нужно иметь внутри "temp.getText (). equals (").O ")" и "temp.getText (). Equals (" X ")", чтобы сделать его там, где он проверяет имя изображения кнопки вместо поиска текста.
Я хочу, чтобы игра сказала, если "X.jpg" (и наоборот для O) уже находится внутри кнопки, тогда вызовите всплывающее окно с этой ошибкой
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.Timer;
import java.util.TimerTask;
public class Controller
{
@FXML
private Text winnerLabel;
@FXML
private Button space1;
@FXML
private Button space2;
@FXML
private Button space3;
@FXML
private Button space4;
@FXML
private Button space5;
@FXML
private Button space6;
@FXML
private Button space7;
@FXML
private Button space8;
@FXML
private Button space9;
@FXML
private Button reset;
@FXML
private Text playerLabel;
@FXML
private Text timerLabel;
@FXML
private Image image;
private boolean isGame = true;
private ImageView tempImageView;
@FXML
protected void initialize()
{
if(isGame)
{
Timer timer = new Timer();
TimerTask task = new TimerTask()
{
@Override
public void run()
{
timerLabel.setText(Integer.toString(Integer.parseInt(timerLabel.getText()) - 1));
if(Integer.parseInt(timerLabel.getText()) == 0)
{
if(playerLabel.getText().equals("Player 1"))
{
playerLabel.setText("Player 2");
}
else
{
playerLabel.setText("Player 1");
}
timerLabel.setText(Integer.toString(Integer.parseInt(timerLabel.getText()+15)));
if(space1.getText() == "X" && space2.getText() == "X" && space3.getText() == "X")
{
space1.setStyle("-fx-background-color:#90EE90");
space2.setStyle("-fx-background-color:#90EE90");
space3.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
isGame=false;
}
else if (space1.getText() == "O" && space2.getText() == "O" && space3.getText() == "O")
{
space1.setStyle("-fx-background-color:#90EE90");
space2.setStyle("-fx-background-color:#90EE90");
space3.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//vertical
else if(space1.getText() == "X" && space4.getText() == "X" && space7.getText() == "X")
{
space1.setStyle("-fx-background-color:#90EE90");
space4.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if(space1.getText() == "O" && space4.getText() == "O" && space7.getText() == "O")
{
space1.setStyle("-fx-background-color:#90EE90");
space4.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//diagonal
else if (space1.getText() == "X" && space5.getText() == "X" && space9.getText() == "X")
{
space1.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space1.getText() == "O" && space5.getText() == "O" && space9.getText() == "O")
{
space1.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//vertical 2nd
else if (space2.getText() == "X" && space5.getText() == "X" && space8.getText() == "X")
{
space2.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space2.getText() == "O" && space5.getText() == "O" && space8.getText() == "O")
{
space2.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//vertical 3rd
else if (space3.getText() == "X" && space6.getText() == "X" && space9.getText() == "X")
{
space3.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if(space3.getText() == "O" && space6.getText() == "O" && space9.getText() == "O")
{
space3.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//Horizontal 2nd
else if (space4.getText() == "X" && space5.getText() == "X" && space6.getText() == "X")
{
space4.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space4.getText() == "O" && space5.getText() == "O" && space6.getText() == "O")
{
space4.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space6.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//Horizontal 3rd
else if (space7.getText() == "X" && space8.getText() == "X" && space9.getText() == "X")
{
space7.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space7.getText() == "O" && space8.getText() == "O" && space9.getText() == "O")
{
space7.setStyle("-fx-background-color:#90EE90");
space8.setStyle("-fx-background-color:#90EE90");
space9.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
//Diagonal 2nd
else if (space3.getText() == "X" && space5.getText() == "X" && space7.getText() == "X")
{
space3.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 1 has won, click reset to play again.");
timer.cancel();
}
else if (space3.getText() == "O" && space5.getText() == "O" && space7.getText() == "O")
{
space3.setStyle("-fx-background-color:#90EE90");
space5.setStyle("-fx-background-color:#90EE90");
space7.setStyle("-fx-background-color:#90EE90");
space1.setDisable(true);
space2.setDisable(true);
space3.setDisable(true);
space4.setDisable(true);
space5.setDisable(true);
space6.setDisable(true);
space7.setDisable(true);
space8.setDisable(true);
space9.setDisable(true);
winnerLabel.setText("Player 2 has won, click reset to play again.");
timer.cancel();
}
}
}
};
timer.schedule(task, 1000, 1000);
}
}
/*
This method is the part of the program that will get which of the 9 buttons was clicked and based on who's player turn it is will change the text of the button to either an
X for player 1 or an O for player 2. afterwards the second player will go
*/
public void playTile(ActionEvent e)
{
Button temp = (Button)e.getSource();
Image tempImageX = new Image(getClass().getResource("X.jpg").toString());
Image tempImageO = new Image(getClass().getResource("O.png").toString());
if(temp.getText().equals("X"))
{
try
{
Stage screen1 = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("errorScreen.fxml"));
screen1.setTitle("Error!");
screen1.setScene(new Scene(root, 300, 275));
screen1.show();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else if (temp.getText().equals("O"))
{
try
{
Stage screen1 = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("errorScreen.fxml"));
screen1.setTitle("Error!");
screen1.setScene(new Scene(root, 300, 275));
screen1.show();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
else
{
if (!temp.isDisable())
{
if (playerLabel.getText().equals("Player 1"))
{
playerLabel.setText("Player 2");
tempImageView.setImage(tempImageX);
temp.setGraphic(tempImageView);
tempImageView.setId("X");
}
else
{
playerLabel.setText("Player 1");
tempImageView.setImage(tempImageO);
temp.setGraphic(tempImageView);
tempImageView.setId("O");
}
}
}
}
/*
This method is the functionality of the reset button when clicked it will
reset all the 9 "Tiles" buttons text to blank and from that also reset the
player label back to "Player 1:
to continue the game.
*/
public void resetButton(ActionEvent event)
{
playerLabel.setText("Player 1");
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
initialize();
timer.cancel();
}
};
timer.schedule(task, 1000, 1000);
winnerLabel.setText("");
space1.setDisable(false);
space2.setDisable(false);
space3.setDisable(false);
space4.setDisable(false);
space5.setDisable(false);
space6.setDisable(false);
space7.setDisable(false);
space8.setDisable(false);
space9.setDisable(false);
space1.setTextFill(Paint.valueOf("000000"));
space2.setTextFill(Paint.valueOf("000000"));
space3.setTextFill(Paint.valueOf("000000"));
space4.setTextFill(Paint.valueOf("000000"));
space5.setTextFill(Paint.valueOf("000000"));
space6.setTextFill(Paint.valueOf("000000"));
space7.setTextFill(Paint.valueOf("000000"));
space8.setTextFill(Paint.valueOf("000000"));
space9.setTextFill(Paint.valueOf("000000"));
space1.setGraphic(null);
space2.setGraphic(null);
space3.setGraphic(null);
space4.setGraphic(null);
space5.setGraphic(null);
space6.setGraphic(null);
space7.setGraphic(null);
space8.setGraphic(null);
space9.setGraphic(null);
}
}