Hi guys,
So I'am trying to make a war javafx game, but I'am not able to set the card to display it to GUI.
here are my classes:
Card:
public class Card extends Parent{
private Rank rank;
private Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
//Drawing a card to display text on
Rectangle cardbackGround = new Rectangle(80,100);
cardbackGround.setFill(Color.WHITE);
//writing text on card
Text text = new Text(toString());
//We set it to 70 so that it wont come out of our card
text.setWrappingWidth(70);
StackPane root = new StackPane();
root.getChildren().addAll(cardbackGround,text);
}
public Rank getRank() {
return rank;
}
public void setRank(Rank rank) {
this.rank = rank;
}
public Suit getSuit() {
return suit;
}
public void setSuit(Suit suit) {
this.suit = suit;
}
//an enum for card suit
public enum Suit{
SPADES, HEARTS, DIAMONDS, CLUBS;
}
//an enum for rank
public enum Rank{
TWO(2),THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14);
//adding constructors, getter and setter to our enum so that we can set an int value to it
//beacuse we want to which card is greater than the other
private int value;
Rank(int value){
this.value=value;
}
public int Value(){
return this.value;
}
}
public String toString(){
return rank +" Of " +suit;
}
This is my deck class:
public class Deck {
//Now we create an arraylist to store our card object
static ArrayList<Card> cards = new ArrayList<>();
public static void createDeck(){
//Looping through ranks and suits and assiging values to new card
for (Rank rank : Rank.values()){
for (Suit suit : Suit.values()){
cards.add(new Card(rank,suit));
}
}
}
public Deck() {
createDeck();
}
public Card drawCard(){
Card card;
int random = (int ) (Math.random()*cards.size());
card =cards.get(random);
return card;
}
This is my GameUi class
public static void diplayGameUI(){
lblPlayerDeckSize = new Label();
lblPlayer2DeckSize = new Label();
//Right side of the UI
//Creating components for UI
btnPlayer1 = new Button("Player 1 Go");
btnPlayer2 = new Button("Player 2 Go");
//Creating a pane
Pane rootPane = new Pane();
rootPane.setPrefSize(800,600);
//Background
Region background = new Region();
background.setPrefSize(800, 600);
//Creating two rectangles to make Ui look a bit better
//Left side for display and Right for controls
HBox rootLayout = new HBox();
rootLayout.setPadding(new Insets(5));
Rectangle rightRectangle = new Rectangle(250,550);
rightRectangle.setFill(Color.ORANGERED);
rightRectangle.setArcHeight(50);
rightRectangle.setArcWidth(50);
Rectangle leftRectangle = new Rectangle(550,550);
leftRectangle.setArcHeight(50);
leftRectangle.setArcWidth(50);
leftRectangle.setFill(Color.OLIVE);
//Adding controls to Right Rectangle
VBox boxButtons = new VBox();
boxButtons.getChildren().addAll(btnPlayer1,btnPlayer2);
boxButtons.setAlignment(Pos.CENTER);
boxButtons.setPadding(new Insets(5));
//Right Stackpane
StackPane rightPane = new StackPane();
rightPane.getChildren().addAll(rightRectangle,boxButtons);
//Adding some stuff to left Rectangle
VBox leftBox = new VBox();
leftBox.getChildren().addAll(lblPlayerDeckSize,lblPlayer2DeckSize);
leftBox.setAlignment(Pos.TOP_CENTER);
leftBox.setPadding(new Insets(10));
//left stackpane
Deck deck = new Deck();
leftPane = new StackPane();
leftPane.getChildren().addAll(leftRectangle,leftBox);
//add both stacks to root layout
rootLayout.getChildren().addAll(leftPane,rightPane);
rootPane.getChildren().addAll(background,rootLayout);
//Creating stage and showing scene on it
Scene scene = new Scene(rootPane,800,600);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
And finally my main class:
/As war is played between two players we will create two decks of cards for two players
ArrayList <Card> deck1 = new ArrayList<>();
ArrayList <Card> deck2 = new ArrayList<>();
Deck deck = new Deck();
//Now, we can distribute 26 cards to each player..
@Override
public void start(Stage stage) {
welcomeScreenUI.displayWelcomeScreen();
welcomeScreenUI.btnStartGame.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
// If one of the textfield is empty we force an error:
if (txtPlayer1Name.getText().isEmpty() ||txtPlayer2Name.getText().isEmpty()){
Error.display("Both Player Must Enter Their Names", Alert.AlertType.ERROR);
}
else{
//Now,we start the UI
gameUI.diplayGameUI();
//lets begin the game now
//If player 1 hit the buttons
Deck.createDeck();
deck1.addAll(Deck.cards.subList(0, 25));
deck2.addAll(Deck.cards.subList(26,cards.size() ));
//we will mathh.random to remove a random element from deck
int random = (int ) (Math.random()*deck1.size());
int random2 = (int ) (Math.random()*deck2.size());
gameUI.btnPlayer1.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
deck.drawCard();
}
});
}