Question: Assume this JAVAfx program will randomly draw 3 Cards and show the images. (assume all pictures of cards are in a folder in the directory
Assume this JAVAfx program will randomly draw 3 Cards and show the images. (assume all pictures of cards are in a folder in the directory called image and they are named (1.png, 2.png, 3.png.........52.png)). Create a BUTTON that will "restart" program and draw again for 3 different cards each time you click it.
///////////////////////////////////////////////////////CARD CLASS///////////////////////////////////////////////////////
public class Card {
private String suit; private String rank; private int value;
public Card(String suit, String rank) { this.suit = suit; this.rank = rank;
if (rank.equals("2")) { value = 2; } else if (rank.equals("3")) { value = 3; } else if (rank.equals("4")) { value = 4; } else if (rank.equals("5")) { value = 5; } else if (rank.equals("6")) { value = 6; } else if (rank.equals("7")) { value = 7; } else if (rank.equals("8")) { value = 8; } else if (rank.equals("9")) { value = 9; } else if (rank.equals("10")) { value = 10; } else if (rank.equals("Jack")) { value = 11; } else if (rank.equals("Queen")) { value = 12; } else if (rank.equals("King")) { value = 13; } else if (rank.equals("Ace")) { value = 14; } }
public String getSuit() { return suit; }
public String getRank() { return rank; }
public int getValue() { return value; }
public void setSuit(String suit) { this.suit = suit; }
public void setRank(String rank) { this.rank = rank; }
public void setValue(int value) { this.value = value; }
public String toString() { return rank + " " + suit; }
public Card copy(Card card) { Card temp = new Card(card.getSuit(), card.getRank()); return temp; }
}
///////////////////////////////////////////////////////DECK CLASS///////////////////////////////////////////////////////
import java.util.ArrayList; import java.util.Random;
public class Deck{ Random random = new Random(); private ArrayList
public Deck() { String suit = ""; String rank = ""; for (int x = 0; x < 4; x++) { if (x == 0) { suit = "Clubs"; } else if (x == 1) { suit = "Hearts"; } else if (x == 2) { suit = "Spades"; } else if (x == 3) { suit = "Diamonds"; } for (int y = 1; y <= 13; y++) { if (y == 1) { rank = "Ace"; } else if (y > 1 && y < 11) { rank = "" + y; } else if (y == 11) { rank = "Jack"; } else if (y == 12) { rank = "Queen"; } else if (y == 13) { rank = "King"; } Card card = new Card(suit, rank); Deck.add(card); } } } public ArrayList
///////////////////////////////////////////////////////DRIVER DRAW CLASS///////////////////////////////////////////////////////
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.stage.Stage; import java.io.FileInputStream; import java.util.ArrayList;
public class Show3Cards extends Application { public static int cardToInt(Card c) { int i = 0; String suit = c.getSuit(); if (suit == "Clubs") { i = 0; } else if (suit == "Hearts") { i = 1; } else if (suit == "Spades") { i = 2; } else if (suit == "Diamonds") { i = 3; } int val = c.getValue(); if (val == 14) { val = 1; } return 13 * i + val; } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("3 Random Cards"); System.out.println("displaying 3 random cards"); Deck dck = new Deck(); dck.shuffle(); ArrayList listOfCards = dck.getShuffled(); for (int i = 0; i < 3; i++) { Card c = (Card) listOfCards.get(i); System.out.println(c); System.out.println(cardToInt(c)); } FileInputStream input; input = new FileInputStream("image/" + cardToInt((Card) listOfCards.get(0)) + ".png"); Image image0 = new Image(input); ImageView imageView0 = new ImageView(image0); input = new FileInputStream("image/" + cardToInt((Card) listOfCards.get(1)) + ".png"); Image image1 = new Image(input); ImageView imageView1 = new ImageView(image1); input = new FileInputStream("image/" + cardToInt((Card) listOfCards.get(2)) + ".png"); Image image2 = new Image(input); ImageView imageView2 = new ImageView(image2); HBox hbox = new HBox(imageView0, imageView1, imageView2); Scene scene = new Scene(hbox, 200, 100); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { Application.launch(args); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
