Skip to content
Snippets Groups Projects
Commit c0469b85 authored by André Wikheim Merkesdal's avatar André Wikheim Merkesdal
Browse files

Added javadoc and finished the application

parent 6c07da14
No related branches found
No related tags found
No related merge requests found
Showing
with 457 additions and 116 deletions
...@@ -56,3 +56,7 @@ u ...@@ -56,3 +56,7 @@ u
Esrc/main/java/edu/ntnu/stud/cardgame/controller/ButtonController.java,8\f\8ff99be84aa52505518939fa5cd13c156bd35d9f Esrc/main/java/edu/ntnu/stud/cardgame/controller/ButtonController.java,8\f\8ff99be84aa52505518939fa5cd13c156bd35d9f
u u
Esrc/main/java/edu/ntnu/stud/cardgame/view/components/OverlayView.java,3\5\35c4072017451a22960f8a35d958c1895f70199c Esrc/main/java/edu/ntnu/stud/cardgame/view/components/OverlayView.java,3\5\35c4072017451a22960f8a35d958c1895f70199c
t
Dsrc/main/java/edu/ntnu/stud/cardgame/util/ImageLoadingException.java,0\9\0975c71ba9476317994e6c45939476748599abe3
v
Fsrc/main/java/edu/ntnu/stud/cardgame/view/components/CustomButton.java,f\e\fece8beb53ba8d32175b4846d92b1535902e95aa
\ No newline at end of file
...@@ -56,3 +56,7 @@ u ...@@ -56,3 +56,7 @@ u
Esrc/main/java/edu/ntnu/stud/cardgame/controller/ButtonController.java,8\f\8ff99be84aa52505518939fa5cd13c156bd35d9f Esrc/main/java/edu/ntnu/stud/cardgame/controller/ButtonController.java,8\f\8ff99be84aa52505518939fa5cd13c156bd35d9f
u u
Esrc/main/java/edu/ntnu/stud/cardgame/view/components/OverlayView.java,3\5\35c4072017451a22960f8a35d958c1895f70199c Esrc/main/java/edu/ntnu/stud/cardgame/view/components/OverlayView.java,3\5\35c4072017451a22960f8a35d958c1895f70199c
t
Dsrc/main/java/edu/ntnu/stud/cardgame/util/ImageLoadingException.java,0\9\0975c71ba9476317994e6c45939476748599abe3
v
Fsrc/main/java/edu/ntnu/stud/cardgame/view/components/CustomButton.java,f\e\fece8beb53ba8d32175b4846d92b1535902e95aa
\ No newline at end of file
...@@ -7,11 +7,31 @@ import javafx.scene.Scene; ...@@ -7,11 +7,31 @@ import javafx.scene.Scene;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.stage.Stage; import javafx.stage.Stage;
/**
* The main class for the Card Game application. This class sets up the primary stage and the
* home frame, and sets up the button actions for the home frame.
* This is where the application runs from.
*
* @author André Merkesdal
* @version 1.0
*/
public class CardGame extends Application { public class CardGame extends Application {
/**
* The main method for the Card Game application. This method launches the application.
*
* @param args The command line arguments.
*/
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
/**
* The start method for the Card Game application. This method sets up the primary stage and the
* home frame, and sets up the button actions for the home frame.
*
* @param primaryStage The primary stage for the application.
*/
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
...@@ -28,6 +48,12 @@ public class CardGame extends Application { ...@@ -28,6 +48,12 @@ public class CardGame extends Application {
primaryStage.show(); primaryStage.show();
} }
/**
* Sets up the button actions for the home frame.
*
* @param homeFrame The home frame for the application.
* @param btnController The button controller for the application.
*/
private void setupButtonActions(HomeFrame homeFrame, ButtonController btnController) { private void setupButtonActions(HomeFrame homeFrame, ButtonController btnController) {
homeFrame.getButtons().actionOnDealButton(e -> btnController.actionOnDealButton()); homeFrame.getButtons().actionOnDealButton(e -> btnController.actionOnDealButton());
homeFrame.getButtons().actionOnCheckButton(e -> btnController.actionOnCheckButton()); homeFrame.getButtons().actionOnCheckButton(e -> btnController.actionOnCheckButton());
......
...@@ -3,24 +3,44 @@ package edu.ntnu.stud.cardgame.controller; ...@@ -3,24 +3,44 @@ package edu.ntnu.stud.cardgame.controller;
import edu.ntnu.stud.cardgame.model.Hand; import edu.ntnu.stud.cardgame.model.Hand;
import edu.ntnu.stud.cardgame.model.PlayingCard; import edu.ntnu.stud.cardgame.model.PlayingCard;
import edu.ntnu.stud.cardgame.view.HomeFrame; import edu.ntnu.stud.cardgame.view.HomeFrame;
import edu.ntnu.stud.cardgame.view.components.Button; import edu.ntnu.stud.cardgame.view.components.CustomButton;
import edu.ntnu.stud.cardgame.view.components.OverlayView; import edu.ntnu.stud.cardgame.view.components.OverlayView;
import javafx.application.Platform;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javafx.application.Platform;
/**
* Controller class for the buttons in the HomeFrame and their actions.
* The class is responsible for creating a new deck of cards, dealing cards, and checking the hand
* when the buttons are pressed.
*
* @author André Merkesdal
* @version 1.0
*/
public class ButtonController { public class ButtonController {
// Class fields
private final DeckOfCards deck; private final DeckOfCards deck;
private Hand hand; private Hand hand;
private HomeFrame homeFrame; private final HomeFrame homeFrame;
private ImageImporter imageImporter;
/**
* Constructor for the ButtonController class.
* Initializes the class fields and creates a new deck of cards.
*
* @param homeFrame The HomeFrame object used in the application.
*/
public ButtonController(HomeFrame homeFrame) { public ButtonController(HomeFrame homeFrame) {
this.imageImporter = new ImageImporter(); ImageImporter imageImporter = new ImageImporter("/edu/ntnu/stud/cardgame/images/");
this.deck = new DeckOfCards(imageImporter); this.deck = new DeckOfCards(imageImporter);
this.homeFrame = homeFrame; this.homeFrame = homeFrame;
} }
/**
* Method for dealing cards from the deck and updating the
* corresponding frames.
*
* @param n The number of cards to deal.
*/
private void dealCards(int n) { private void dealCards(int n) {
this.hand = new Hand(deck.dealHand(n)); this.hand = new Hand(deck.dealHand(n));
homeFrame.getCheckedHand().clear(); homeFrame.getCheckedHand().clear();
...@@ -28,17 +48,15 @@ public class ButtonController { ...@@ -28,17 +48,15 @@ public class ButtonController {
homeFrame.getCardFrame().addCards(hand); homeFrame.getCardFrame().addCards(hand);
} }
public void actionOnDealButton() { /**
OverlayView overlayView = new OverlayView(); * Private method for creating a deal button with a given number of cards to deal.
overlayView.addButtons(createDealButton(5, overlayView), createDealButton(10, overlayView), createDealButton(15, overlayView)); *
* @param n The number of cards to deal.
overlayView.getOverlay().setOnMouseClicked(e -> homeFrame.getChildren().remove(overlayView.getOverlay())); * @param overlayView The overlay view to add the button to.
* @return The created button.
homeFrame.getChildren().add(overlayView.getOverlay()); */
} private CustomButton createDealButton(int n, OverlayView overlayView) {
CustomButton button = new CustomButton("Deal " + n);
private Button createDealButton(int n, OverlayView overlayView) {
Button button = new Button("Deal " + n);
button.setOnAction(e -> { button.setOnAction(e -> {
dealCards(n); dealCards(n);
homeFrame.getChildren().remove(overlayView.getOverlay()); homeFrame.getChildren().remove(overlayView.getOverlay());
...@@ -46,6 +64,23 @@ public class ButtonController { ...@@ -46,6 +64,23 @@ public class ButtonController {
return button; return button;
} }
/**
* Decides the action to be taken when the deal button is pressed.
*/
public void actionOnDealButton() {
OverlayView overlayView = new OverlayView();
overlayView.addButtons(createDealButton(5, overlayView),
createDealButton(10, overlayView), createDealButton(15, overlayView));
overlayView.getOverlay().setOnMouseClicked(e ->
homeFrame.getChildren().remove(overlayView.getOverlay()));
homeFrame.getChildren().add(overlayView.getOverlay());
}
/**
* Decides the action to be taken when the check button is pressed.
*/
public void actionOnCheckButton() { public void actionOnCheckButton() {
homeFrame.getCheckedHand().clear(); homeFrame.getCheckedHand().clear();
homeFrame.getCheckedHand().setSumField(Integer.toString(hand.sumOfFaces())); homeFrame.getCheckedHand().setSumField(Integer.toString(hand.sumOfFaces()));
...@@ -62,6 +97,9 @@ public class ButtonController { ...@@ -62,6 +97,9 @@ public class ButtonController {
homeFrame.getCheckedHand().setFlushField(flush); homeFrame.getCheckedHand().setFlushField(flush);
} }
/**
* Decides the action to be taken when the quit button is pressed.
*/
public void actionOnQuitButton() { public void actionOnQuitButton() {
Platform.exit(); Platform.exit();
} }
......
package edu.ntnu.stud.cardgame.controller; package edu.ntnu.stud.cardgame.controller;
import edu.ntnu.stud.cardgame.model.Hand;
import edu.ntnu.stud.cardgame.model.PlayingCard; import edu.ntnu.stud.cardgame.model.PlayingCard;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Random; import java.util.Random;
import java.util.stream.Collectors;
/**
* A deck of playing cards. The deck is initialized with 52 cards, and cards can be dealt from the
* deck.
*
* @author André Merkesdal
* @version 1.0
*/
public class DeckOfCards { public class DeckOfCards {
private static final char[] SUITS = {'H', 'D', 'S', 'C'};
private PlayingCard[] cards; // Class fields
private Random random; private final PlayingCard[] cards;
private final Random random;
private static final char[] SUITS = {'H', 'D', 'S', 'C'};
/**
* Constructs a new deck of cards. Takes an image importer as an argument to import images for the
* cards used in the game to create the cards with their images.
*
* @param imageImporter an image importer to import images for the cards used in the game.
*/
public DeckOfCards(ImageImporter imageImporter) { public DeckOfCards(ImageImporter imageImporter) {
this.cards = new PlayingCard[52]; this.cards = new PlayingCard[52];
this.random = new Random(); this.random = new Random();
...@@ -26,6 +36,14 @@ public class DeckOfCards { ...@@ -26,6 +36,14 @@ public class DeckOfCards {
} }
} }
/**
* Deals a hand of n cards from the deck. The cards are chosen randomly from the deck, and the
* cards are removed from the deck.
*
* @param n the number of cards to deal.
* @return a collection of n cards.
* @throws IllegalArgumentException if n is less than 0 or greater than 52.
*/
public Collection<PlayingCard> dealHand(int n) { public Collection<PlayingCard> dealHand(int n) {
if (n < 0 || n > 52) { if (n < 0 || n > 52) {
throw new IllegalArgumentException("Invalid number of cards: " + n); throw new IllegalArgumentException("Invalid number of cards: " + n);
......
package edu.ntnu.stud.cardgame.controller; package edu.ntnu.stud.cardgame.controller;
import edu.ntnu.stud.cardgame.model.PlayingCard; import edu.ntnu.stud.cardgame.util.ImageLoadingException;
import javafx.scene.image.Image;
import java.io.InputStream; import java.io.InputStream;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.scene.image.Image;
/**
* Class for importing images from the file system and storing them in a map for easy access.
* The class is used to import images of playing cards for the card game application.
*
* @author André Merkesdal
* @version 1.0
*/
public class ImageImporter { public class ImageImporter {
// Class fields
private static final char[] SUITS = {'H', 'D', 'S', 'C'};
private final String path;
Map<String, Image> images; Map<String, Image> images;
public ImageImporter() { /**
* Constructor for the ImageImporter class.
* Initializes the class fields and imports the images from the file system.
*
* @param path The path to the directory containing the images.
*/
public ImageImporter(String path) {
this.path = path;
this.images = new HashMap<>(); this.images = new HashMap<>();
this.initialize(); this.initialize();
} }
/**
* Method for importing images from the file system and storing them in a map.
*/
public void initialize() { public void initialize() {
String path = "/edu/ntnu/stud/cardgame/images/";
List<String> files = new ArrayList<>(); List<String> files = new ArrayList<>();
char[] suits = {'H', 'D', 'S', 'C'}; for (char suit : SUITS) {
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) { for (int j = 1; j <= 13; j++) {
files.add(path + suits[i] + j + ".png"); files.add(path + suit + j + ".png");
} }
} }
for (String file : files) { for (String file : files) {
InputStream stream = getClass().getResourceAsStream(file); try (InputStream stream = getClass().getResourceAsStream(file)) {
if (stream == null) { if (stream == null) {
throw new RuntimeException("Failed to load image file: " + file); throw new ImageLoadingException("Failed to load image file: " + file);
} }
Image image = new Image(stream); Image image = new Image(stream);
images.put(getFilenameWithoutExtension(file), image); images.put(getFilenameWithoutExtension(file), image);
} catch (Exception e) {
throw new ImageLoadingException("Failed to load image file: " + file, e);
}
} }
} }
/**
* Private method for extracting the filename from a path without the file extension.
* Used to create keys for the image map.
*
* @param path The path to the file.
* @return The filename without the file extension.
*/
private String getFilenameWithoutExtension(String path) { private String getFilenameWithoutExtension(String path) {
String file = path.substring(path.lastIndexOf("/") + 1); String file = path.substring(path.lastIndexOf("/") + 1);
return file.substring(0, file.lastIndexOf(".")); return file.substring(0, file.lastIndexOf("."));
} }
/**
* Method for retrieving an image from the map using a key.
*
* @param key The key for the image in the map.
* @return The image corresponding to the key.
*/
public Image getImage(String key) { public Image getImage(String key) {
Image image = images.get(key); Image image = images.get(key);
if (image == null) { if (image == null) {
......
...@@ -3,25 +3,33 @@ package edu.ntnu.stud.cardgame.model; ...@@ -3,25 +3,33 @@ package edu.ntnu.stud.cardgame.model;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
public class Hand { /**
private Collection<PlayingCard> cards; * Class representing a hand of playing cards.
* The class is used to represent a hand of playing cards and contains methods for checking the
public Hand(Collection<PlayingCard> cards) { * hand for specific combinations. This class is a Record class because it is a simple data
this.cards = cards; * container with no behavior.
} *
* @author André Merkesdal
public Collection<PlayingCard> getCards() { * @version 1.0
return cards; */
} public record Hand(Collection<PlayingCard> cards) {
/**
* Method for calculating the sum of the faces of the cards in the hand.
*
* @return The sum of the faces of the cards in the hand.
*/
public int sumOfFaces() { public int sumOfFaces() {
int sum = 0; return cards.stream()
for (PlayingCard card : cards) { .mapToInt(PlayingCard::getFace)
sum += card.getFace(); .sum();
}
return sum;
} }
/**
* Method for getting all the cards of hearts in the hand.
*
* @return A collection of all the cards of hearts in the hand.
*/
public Collection<PlayingCard> getCardsOfHearts() { public Collection<PlayingCard> getCardsOfHearts() {
Collection<PlayingCard> hearts = new ArrayList<>(); Collection<PlayingCard> hearts = new ArrayList<>();
for (PlayingCard card : cards) { for (PlayingCard card : cards) {
...@@ -32,6 +40,11 @@ public class Hand { ...@@ -32,6 +40,11 @@ public class Hand {
return hearts; return hearts;
} }
/**
* Method for checking if the hand contains the queen of spades.
*
* @return True if the hand contains the queen of spades, false otherwise.
*/
public boolean hasQueenOfSpades() { public boolean hasQueenOfSpades() {
for (PlayingCard card : cards) { for (PlayingCard card : cards) {
if (card.getSuit() == 'S' && card.getFace() == 12) { if (card.getSuit() == 'S' && card.getFace() == 12) {
...@@ -41,6 +54,11 @@ public class Hand { ...@@ -41,6 +54,11 @@ public class Hand {
return false; return false;
} }
/**
* Method for checking if the hand contains a flush.
*
* @return True if the hand contains a flush, false otherwise.
*/
public boolean isFlush() { public boolean isFlush() {
char suit = cards.iterator().next().getSuit(); char suit = cards.iterator().next().getSuit();
for (PlayingCard card : cards) { for (PlayingCard card : cards) {
......
...@@ -9,13 +9,13 @@ import javafx.scene.image.ImageView; ...@@ -9,13 +9,13 @@ import javafx.scene.image.ImageView;
* The card can also be one of 4 suits: Spade, Heart, Diamonds and Clubs. * The card can also be one of 4 suits: Spade, Heart, Diamonds and Clubs.
* *
* @author ntnu * @author ntnu
* @version 2021-03-13 * @version 1.0
*/ */
public class PlayingCard { public class PlayingCard {
// Class fields
private final char suit; // 'S'=spade, 'H'=heart, 'D'=diamonds, 'C'=clubs private final char suit; // 'S'=spade, 'H'=heart, 'D'=diamonds, 'C'=clubs
private final int face; // a number between 1 and 13 private final int face; // a number between 1 and 13
private final ImageView imageView; private final ImageView imageView;
/** /**
...@@ -29,6 +29,7 @@ public class PlayingCard { ...@@ -29,6 +29,7 @@ public class PlayingCard {
* @param suit The suit of the card, as a single character. 'S' for Spades, * @param suit The suit of the card, as a single character. 'S' for Spades,
* 'H' for Heart, 'D' for Diamonds and 'C' for clubs * 'H' for Heart, 'D' for Diamonds and 'C' for clubs
* @param face The face value of the card, an integer between 1 and 13 * @param face The face value of the card, an integer between 1 and 13
* @param image The image of the card, as an Image object
* @throws IllegalArgumentException if suit or face have invalid values. * @throws IllegalArgumentException if suit or face have invalid values.
*/ */
public PlayingCard(char suit, int face, Image image) { public PlayingCard(char suit, int face, Image image) {
......
package edu.ntnu.stud.cardgame.util;
/**
* Custom exception class for handling image loading exceptions.
* The class is used to handle exceptions when importing images from the file system.
*
* @author André Merkesdal
* @version 1.0
*/
public class ImageLoadingException extends RuntimeException {
/**
* Constructor for the ImageLoadingException class.
*
* @param message The message to be displayed when the exception is thrown.
*/
public ImageLoadingException(String message) {
super(message);
}
/**
* Constructor for the ImageLoadingException class.
*
* @param message The message to be displayed when the exception is thrown.
* @param cause The cause of the exception.
*/
public ImageLoadingException(String message, Throwable cause) {
super(message, cause);
}
}
\ No newline at end of file
...@@ -3,17 +3,30 @@ package edu.ntnu.stud.cardgame.view; ...@@ -3,17 +3,30 @@ package edu.ntnu.stud.cardgame.view;
import edu.ntnu.stud.cardgame.view.components.Buttons; import edu.ntnu.stud.cardgame.view.components.Buttons;
import edu.ntnu.stud.cardgame.view.components.CardFrame; import edu.ntnu.stud.cardgame.view.components.CardFrame;
import edu.ntnu.stud.cardgame.view.components.ChecksForHand; import edu.ntnu.stud.cardgame.view.components.ChecksForHand;
import java.util.Objects;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import java.util.Objects; /**
* The main frame of the game. Contains the card frame, buttons and checks for hand.
* This is the main frame of the game where all the other
* components are added.
*
* @author André Merkesdal
* @version 1.0
*/
public class HomeFrame extends StackPane { public class HomeFrame extends StackPane {
private CardFrame cardFrame;
private Buttons buttons;
private ChecksForHand checkedHand;
// The card frame, buttons and additional info for the hand.
private final CardFrame cardFrame;
private final Buttons buttons;
private final ChecksForHand checkedHand;
/**
* Creates a new HomeFrame. Sets the size and adds the card frame, buttons and checks for hand.
* Also adds the global stylesheet.
*/
public HomeFrame() { public HomeFrame() {
this.setPrefSize(1000, 800); this.setPrefSize(1000, 800);
...@@ -29,6 +42,11 @@ public class HomeFrame extends StackPane { ...@@ -29,6 +42,11 @@ public class HomeFrame extends StackPane {
this.getChildren().add(getContent()); this.getChildren().add(getContent());
} }
/**
* Creates the content of the frame. Adds the card frame, buttons and checks for hand.
*
* @return The content of the frame.
*/
private GridPane getContent() { private GridPane getContent() {
GridPane content = new GridPane(); GridPane content = new GridPane();
...@@ -43,16 +61,30 @@ public class HomeFrame extends StackPane { ...@@ -43,16 +61,30 @@ public class HomeFrame extends StackPane {
return content; return content;
} }
/**
* Get the buttons.
*
* @return The buttons.
*/
public Buttons getButtons() { public Buttons getButtons() {
return buttons; return buttons;
} }
/**
* Get the checked hand.
*
* @return The checked hand.
*/
public ChecksForHand getCheckedHand() { public ChecksForHand getCheckedHand() {
return checkedHand; return checkedHand;
} }
/**
* Get the card frame.
*
* @return The card frame.
*/
public CardFrame getCardFrame() { public CardFrame getCardFrame() {
return cardFrame; return cardFrame;
} }
} }
...@@ -5,16 +5,31 @@ import javafx.event.EventHandler; ...@@ -5,16 +5,31 @@ import javafx.event.EventHandler;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
/**
* A class for the buttons in the game used to deal, check and quit the game.
* The class extends VBox and contains three buttons.
*
* @author André Merkesdal
* @version 1.0
*/
public class Buttons extends VBox { public class Buttons extends VBox {
private final Button deal;
private final Button check; // The buttons used in the game.
private final Button quit; private final CustomButton deal;
private final CustomButton check;
private final CustomButton quit;
/**
* Constructor for the Buttons class.
* Sets the padding for the VBox and adds the buttons to the VBox.
* Also sets the margins for the buttons.
*/
public Buttons() { public Buttons() {
this.setPadding(new Insets(15)); this.setPadding(new Insets(15));
this.deal = new Button("Deal Hand"); this.deal = new CustomButton("Deal Hand");
this.check = new Button("Check Hand"); this.check = new CustomButton("Check Hand");
this.quit = new Button("Quit Game"); this.quit = new CustomButton("Quit Game");
this.getChildren().addAll(deal, check, quit); this.getChildren().addAll(deal, check, quit);
...@@ -22,17 +37,30 @@ public class Buttons extends VBox { ...@@ -22,17 +37,30 @@ public class Buttons extends VBox {
VBox.setMargin(quit, new Insets(240, 0, 0, 0)); VBox.setMargin(quit, new Insets(240, 0, 0, 0));
} }
/**
* Method for setting the action on the deal button.
*
* @param event The event to be set on the deal button.
*/
public void actionOnDealButton(EventHandler<ActionEvent> event) { public void actionOnDealButton(EventHandler<ActionEvent> event) {
deal.setOnAction(event); deal.setOnAction(event);
} }
/**
* Method for setting the action on the check button.
*
* @param event The event to be set on the check button.
*/
public void actionOnCheckButton(EventHandler<ActionEvent> event) { public void actionOnCheckButton(EventHandler<ActionEvent> event) {
check.setOnAction(event); check.setOnAction(event);
} }
/**
* Method for setting the action on the quit button.
*
* @param event The event to be set on the quit button.
*/
public void actionOnQuitButton(EventHandler<ActionEvent> event) { public void actionOnQuitButton(EventHandler<ActionEvent> event) {
quit.setOnAction(event); quit.setOnAction(event);
} }
} }
...@@ -6,11 +6,17 @@ import javafx.geometry.Pos; ...@@ -6,11 +6,17 @@ import javafx.geometry.Pos;
import javafx.scene.layout.TilePane; import javafx.scene.layout.TilePane;
/** /**
* A frame for displaying cards. * A frame for displaying cards. This class is a subclass of TilePane.
* It is used to display a hand of cards in a graphical user interface.
* *
* @version 2021-03-29 * @author André Merkesdal
* @version 1.0
*/ */
public class CardFrame extends TilePane { public class CardFrame extends TilePane {
/**
* Constructs a new CardFrame object.
*/
public CardFrame() { public CardFrame() {
this.setMinSize(700, 500); this.setMinSize(700, 500);
this.setMaxSize(700, 500); this.setMaxSize(700, 500);
...@@ -18,12 +24,20 @@ public class CardFrame extends TilePane { ...@@ -18,12 +24,20 @@ public class CardFrame extends TilePane {
this.setAlignment(Pos.CENTER); this.setAlignment(Pos.CENTER);
} }
/**
* Adds the cards from a hand to the CardFrame.
*
* @param hand The hand to add cards from.
*/
public void addCards(Hand hand) { public void addCards(Hand hand) {
hand.getCards().stream() hand.cards().stream()
.map(PlayingCard::getImage) .map(PlayingCard::getImage)
.forEach(this.getChildren()::add); .forEach(this.getChildren()::add);
} }
/**
* Removes all cards from the CardFrame and clears it.
*/
public void clear() { public void clear() {
this.getChildren().clear(); this.getChildren().clear();
} }
......
...@@ -4,12 +4,24 @@ import javafx.scene.control.TextField; ...@@ -4,12 +4,24 @@ import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import javafx.scene.text.Text; import javafx.scene.text.Text;
/**
* A class to get additional information about a hand.
*
* @author André Merkesdal
* @version 1.0
*/
public class ChecksForHand extends GridPane { public class ChecksForHand extends GridPane {
private TextField sumField;
private TextField heartsField;
private TextField queenOfSpadesField;
private TextField flushField;
// Class fields
private final TextField sumField;
private final TextField heartsField;
private final TextField queenOfSpadesField;
private final TextField flushField;
/**
* Constructor for the ChecksForHand class. Sets up the grid pane and adds the
* necessary fields.
*/
public ChecksForHand() { public ChecksForHand() {
this.setHgap(20); this.setHgap(20);
this.setVgap(10); this.setVgap(10);
...@@ -41,22 +53,45 @@ public class ChecksForHand extends GridPane { ...@@ -41,22 +53,45 @@ public class ChecksForHand extends GridPane {
this.add(flushField, 1, 3); this.add(flushField, 1, 3);
} }
/**
* Method to set the sum field with the sum of the faces of the hand.
*
* @param sum The sum to set.
*/
public void setSumField(String sum) { public void setSumField(String sum) {
sumField.setText(sum); sumField.setText(sum);
} }
/**
* Method to set the hearts field with the number of cards of Hearts in the hand.
*
* @param hearts The number of cards of Hearts to set.
*/
public void setHeartsField(String hearts) { public void setHeartsField(String hearts) {
heartsField.setText(hearts); heartsField.setText(hearts);
} }
/**
* Method to set the Queen of Spades field if the hand includes the Queen of Spades.
*
* @param queenOfSpades Yes if the hand includes the Queen of Spades, otherwise No.
*/
public void setQueenOfSpadesField(String queenOfSpades) { public void setQueenOfSpadesField(String queenOfSpades) {
queenOfSpadesField.setText(queenOfSpades); queenOfSpadesField.setText(queenOfSpades);
} }
/**
* Method to set the flush field if the hand is a flush.
*
* @param flush Yes if the hand is a flush, otherwise No.
*/
public void setFlushField(String flush) { public void setFlushField(String flush) {
flushField.setText(flush); flushField.setText(flush);
} }
/**
* Method to clear all the fields.
*/
public void clear() { public void clear() {
sumField.clear(); sumField.clear();
heartsField.clear(); heartsField.clear();
......
package edu.ntnu.stud.cardgame.view.components; package edu.ntnu.stud.cardgame.view.components;
public class Button extends javafx.scene.control.Button { /**
* A custom button with a name and styling for the game.
*
* @author André Merkesdal
* @version 1.0
*/
public class CustomButton extends javafx.scene.control.Button {
// The name of the button
private final String name; private final String name;
public Button(String name) { /**
* Creates a new custom button with the given name.
*
* @param name The name of the button
*/
public CustomButton(String name) {
this.name = name; this.name = name;
this.setText(name); this.setText(name);
this.getStyleClass().add("button"); this.getStyleClass().add("button");
...@@ -11,6 +24,11 @@ public class Button extends javafx.scene.control.Button { ...@@ -11,6 +24,11 @@ public class Button extends javafx.scene.control.Button {
this.setMinSize(160, 60); this.setMinSize(160, 60);
} }
/**
* Returns the name of the button.
*
* @return The name of the button
*/
public String getName() { public String getName() {
return name; return name;
} }
......
...@@ -5,14 +5,30 @@ import javafx.scene.control.Button; ...@@ -5,14 +5,30 @@ import javafx.scene.control.Button;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
/**
* A view for an overlay with buttons.
*
* @author André Merkesdal
* @version 1.0
*/
public class OverlayView { public class OverlayView {
private StackPane overlay;
// The overlay
private final StackPane overlay;
/**
* Creates a new overlay view.
*/
public OverlayView() { public OverlayView() {
this.overlay = new StackPane(); this.overlay = new StackPane();
this.overlay.setStyle("-fx-background-color: rgba(0,0,0,0.49);"); this.overlay.setStyle("-fx-background-color: rgba(0,0,0,0.49);");
} }
/**
* Adds buttons to the overlay.
*
* @param buttons The buttons to add.
*/
public void addButtons(Button... buttons) { public void addButtons(Button... buttons) {
VBox buttonBox = new VBox(); VBox buttonBox = new VBox();
buttonBox.getStyleClass().add("content"); buttonBox.getStyleClass().add("content");
...@@ -23,6 +39,11 @@ public class OverlayView { ...@@ -23,6 +39,11 @@ public class OverlayView {
this.overlay.getChildren().add(buttonBox); this.overlay.getChildren().add(buttonBox);
} }
/**
* Get the overlay.
*
* @return The overlay.
*/
public StackPane getOverlay() { public StackPane getOverlay() {
return this.overlay; return this.overlay;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment