Checking the graphic of a button
I had a question regarding JavaFX and the ability to check a graphic. so within my program, I have a graphics being set for my buttons when the user clicks it.
What I need to find out is what do I need to have inside "temp.getText().equals("O")" and "temp.getText().equals("X")" to make it to where it's checking the button's image name instead of looking for text.
I want the game to say if "X.jpg"(and vice versa for O) is already inside a button then call this error screen popup
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);
}
}
javafx
add a comment |
I had a question regarding JavaFX and the ability to check a graphic. so within my program, I have a graphics being set for my buttons when the user clicks it.
What I need to find out is what do I need to have inside "temp.getText().equals("O")" and "temp.getText().equals("X")" to make it to where it's checking the button's image name instead of looking for text.
I want the game to say if "X.jpg"(and vice versa for O) is already inside a button then call this error screen popup
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);
}
}
javafx
AnImageView
does not save reference to the image's filename, so unless you store the filename in a variable somewhere, you will not be able to retrieve it. Instead, adding a property to theButton
as Sai Dandem's answer below illustrates may be a feasible option.
– Zephyr
Nov 21 at 1:05
add a comment |
I had a question regarding JavaFX and the ability to check a graphic. so within my program, I have a graphics being set for my buttons when the user clicks it.
What I need to find out is what do I need to have inside "temp.getText().equals("O")" and "temp.getText().equals("X")" to make it to where it's checking the button's image name instead of looking for text.
I want the game to say if "X.jpg"(and vice versa for O) is already inside a button then call this error screen popup
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);
}
}
javafx
I had a question regarding JavaFX and the ability to check a graphic. so within my program, I have a graphics being set for my buttons when the user clicks it.
What I need to find out is what do I need to have inside "temp.getText().equals("O")" and "temp.getText().equals("X")" to make it to where it's checking the button's image name instead of looking for text.
I want the game to say if "X.jpg"(and vice versa for O) is already inside a button then call this error screen popup
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);
}
}
javafx
javafx
asked Nov 20 at 22:09
Raiden Ninja
61
61
AnImageView
does not save reference to the image's filename, so unless you store the filename in a variable somewhere, you will not be able to retrieve it. Instead, adding a property to theButton
as Sai Dandem's answer below illustrates may be a feasible option.
– Zephyr
Nov 21 at 1:05
add a comment |
AnImageView
does not save reference to the image's filename, so unless you store the filename in a variable somewhere, you will not be able to retrieve it. Instead, adding a property to theButton
as Sai Dandem's answer below illustrates may be a feasible option.
– Zephyr
Nov 21 at 1:05
An
ImageView
does not save reference to the image's filename, so unless you store the filename in a variable somewhere, you will not be able to retrieve it. Instead, adding a property to the Button
as Sai Dandem's answer below illustrates may be a feasible option.– Zephyr
Nov 21 at 1:05
An
ImageView
does not save reference to the image's filename, so unless you store the filename in a variable somewhere, you will not be able to retrieve it. Instead, adding a property to the Button
as Sai Dandem's answer below illustrates may be a feasible option.– Zephyr
Nov 21 at 1:05
add a comment |
2 Answers
2
active
oldest
votes
As far as I understand the question, you dont want to set the text to the buttons and you want to check what button it is based on the graphic it has(either X or O).
If this is the question, I would recommend to set/update a property of the button based on the type of the image/graphic it is holding, something like
button.getProperties().put("TYPE","X"); // For X button
button.getProperties().put("TYPE","O"); // For O button
And you can check directly for the type, rather than accessing the graphic. like..
if(space1.getProperties().get("TYPE").equals("X") && space2.getProperties().get("TYPE").equals("X") && ...){
...
}
Alternatively you can utilize the setUserData() method of the Node which internally uses the above logic only. If you go with userData your code will be less verbose also.
button.setUserData("X");
if(space1.getUserData().equals("X") && space2.getUserData().equals("X") && ...){
...
}
add a comment |
There is no way to keep the file name information in an ImageView
or Image
. Of course, you can always extend from either of these 2 classes and store the file name as a field, but you probably don't need that.
You can also keep the references of the two Image
instances and check for reference instead.
Image imageX = new Image(getClass().getResource("X.jpg").toString());
Image imageO = new Image(getClass().getResource("O.png").toString());
public void playTile(ActionEvent e)
{
// Other stuff
Image currentImage = tempImageView.getImage();
if (currentImage == imageX || currentImage == imageO) // Checking for reference
{
// Your logic
}
else
{
// Your logic
}
}
The most interesting thing here is that all you wanted to do is to check whether the "tile" has been filled. The simplest way to do this is actually to do if (tempImageView.getImage() == null)
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402346%2fchecking-the-graphic-of-a-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As far as I understand the question, you dont want to set the text to the buttons and you want to check what button it is based on the graphic it has(either X or O).
If this is the question, I would recommend to set/update a property of the button based on the type of the image/graphic it is holding, something like
button.getProperties().put("TYPE","X"); // For X button
button.getProperties().put("TYPE","O"); // For O button
And you can check directly for the type, rather than accessing the graphic. like..
if(space1.getProperties().get("TYPE").equals("X") && space2.getProperties().get("TYPE").equals("X") && ...){
...
}
Alternatively you can utilize the setUserData() method of the Node which internally uses the above logic only. If you go with userData your code will be less verbose also.
button.setUserData("X");
if(space1.getUserData().equals("X") && space2.getUserData().equals("X") && ...){
...
}
add a comment |
As far as I understand the question, you dont want to set the text to the buttons and you want to check what button it is based on the graphic it has(either X or O).
If this is the question, I would recommend to set/update a property of the button based on the type of the image/graphic it is holding, something like
button.getProperties().put("TYPE","X"); // For X button
button.getProperties().put("TYPE","O"); // For O button
And you can check directly for the type, rather than accessing the graphic. like..
if(space1.getProperties().get("TYPE").equals("X") && space2.getProperties().get("TYPE").equals("X") && ...){
...
}
Alternatively you can utilize the setUserData() method of the Node which internally uses the above logic only. If you go with userData your code will be less verbose also.
button.setUserData("X");
if(space1.getUserData().equals("X") && space2.getUserData().equals("X") && ...){
...
}
add a comment |
As far as I understand the question, you dont want to set the text to the buttons and you want to check what button it is based on the graphic it has(either X or O).
If this is the question, I would recommend to set/update a property of the button based on the type of the image/graphic it is holding, something like
button.getProperties().put("TYPE","X"); // For X button
button.getProperties().put("TYPE","O"); // For O button
And you can check directly for the type, rather than accessing the graphic. like..
if(space1.getProperties().get("TYPE").equals("X") && space2.getProperties().get("TYPE").equals("X") && ...){
...
}
Alternatively you can utilize the setUserData() method of the Node which internally uses the above logic only. If you go with userData your code will be less verbose also.
button.setUserData("X");
if(space1.getUserData().equals("X") && space2.getUserData().equals("X") && ...){
...
}
As far as I understand the question, you dont want to set the text to the buttons and you want to check what button it is based on the graphic it has(either X or O).
If this is the question, I would recommend to set/update a property of the button based on the type of the image/graphic it is holding, something like
button.getProperties().put("TYPE","X"); // For X button
button.getProperties().put("TYPE","O"); // For O button
And you can check directly for the type, rather than accessing the graphic. like..
if(space1.getProperties().get("TYPE").equals("X") && space2.getProperties().get("TYPE").equals("X") && ...){
...
}
Alternatively you can utilize the setUserData() method of the Node which internally uses the above logic only. If you go with userData your code will be less verbose also.
button.setUserData("X");
if(space1.getUserData().equals("X") && space2.getUserData().equals("X") && ...){
...
}
answered Nov 20 at 22:41
Sai Dandem
92128
92128
add a comment |
add a comment |
There is no way to keep the file name information in an ImageView
or Image
. Of course, you can always extend from either of these 2 classes and store the file name as a field, but you probably don't need that.
You can also keep the references of the two Image
instances and check for reference instead.
Image imageX = new Image(getClass().getResource("X.jpg").toString());
Image imageO = new Image(getClass().getResource("O.png").toString());
public void playTile(ActionEvent e)
{
// Other stuff
Image currentImage = tempImageView.getImage();
if (currentImage == imageX || currentImage == imageO) // Checking for reference
{
// Your logic
}
else
{
// Your logic
}
}
The most interesting thing here is that all you wanted to do is to check whether the "tile" has been filled. The simplest way to do this is actually to do if (tempImageView.getImage() == null)
.
add a comment |
There is no way to keep the file name information in an ImageView
or Image
. Of course, you can always extend from either of these 2 classes and store the file name as a field, but you probably don't need that.
You can also keep the references of the two Image
instances and check for reference instead.
Image imageX = new Image(getClass().getResource("X.jpg").toString());
Image imageO = new Image(getClass().getResource("O.png").toString());
public void playTile(ActionEvent e)
{
// Other stuff
Image currentImage = tempImageView.getImage();
if (currentImage == imageX || currentImage == imageO) // Checking for reference
{
// Your logic
}
else
{
// Your logic
}
}
The most interesting thing here is that all you wanted to do is to check whether the "tile" has been filled. The simplest way to do this is actually to do if (tempImageView.getImage() == null)
.
add a comment |
There is no way to keep the file name information in an ImageView
or Image
. Of course, you can always extend from either of these 2 classes and store the file name as a field, but you probably don't need that.
You can also keep the references of the two Image
instances and check for reference instead.
Image imageX = new Image(getClass().getResource("X.jpg").toString());
Image imageO = new Image(getClass().getResource("O.png").toString());
public void playTile(ActionEvent e)
{
// Other stuff
Image currentImage = tempImageView.getImage();
if (currentImage == imageX || currentImage == imageO) // Checking for reference
{
// Your logic
}
else
{
// Your logic
}
}
The most interesting thing here is that all you wanted to do is to check whether the "tile" has been filled. The simplest way to do this is actually to do if (tempImageView.getImage() == null)
.
There is no way to keep the file name information in an ImageView
or Image
. Of course, you can always extend from either of these 2 classes and store the file name as a field, but you probably don't need that.
You can also keep the references of the two Image
instances and check for reference instead.
Image imageX = new Image(getClass().getResource("X.jpg").toString());
Image imageO = new Image(getClass().getResource("O.png").toString());
public void playTile(ActionEvent e)
{
// Other stuff
Image currentImage = tempImageView.getImage();
if (currentImage == imageX || currentImage == imageO) // Checking for reference
{
// Your logic
}
else
{
// Your logic
}
}
The most interesting thing here is that all you wanted to do is to check whether the "tile" has been filled. The simplest way to do this is actually to do if (tempImageView.getImage() == null)
.
answered Nov 21 at 2:29
Jai
5,66411131
5,66411131
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402346%2fchecking-the-graphic-of-a-button%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
An
ImageView
does not save reference to the image's filename, so unless you store the filename in a variable somewhere, you will not be able to retrieve it. Instead, adding a property to theButton
as Sai Dandem's answer below illustrates may be a feasible option.– Zephyr
Nov 21 at 1:05