i have created a program that will randomly select 5 cards and display the output as a string.. I would like to modify that code to also display a graphical representation for each return value. The problem is that I'm not sure how to move forward.
My thoughts: create a new imageicon for each card value.
but thats as far as i get. How do i tell java "if the returned values are 2club, 4 hearts, 6 spade, 3 diamond and Ace heart" then display 2club.gif, 4heart.gif etc.
here;s my code so far.......
and my main class
My thoughts: create a new imageicon for each card value.
but thats as far as i get. How do i tell java "if the returned values are 2club, 4 hearts, 6 spade, 3 diamond and Ace heart" then display 2club.gif, 4heart.gif etc.
here;s my code so far.......
Code:
public class Card
{
//define class variables
private int face;
private int suit;
private String faceName = "Ace, King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five, Four, Three, Two";
private String suitName = "Heart, Diamond, Spade, Club";
ImageIcon c1icon = new ImageIcon ("clubs-2-75.jpg");
ImageIcon c2icon = new ImageIcon ("clubs-3-75.jpg");
ImageIcon c3icon = new ImageIcon ("clubs-4-75.jpg");
ImageIcon c4icon = new ImageIcon ("clubs-5-75.jpg");
ImageIcon c5icon = new ImageIcon ("clubs-6-75.jpg");
ImageIcon c6icon = new ImageIcon ("clubs-7-75.jpg");
/*
Sets the initial face value and is the same name as the class.
*/
public Card()
{
face = (int) (Math.random() * 13 + 1); //create a random value for the face values
suit = (int) (Math.random() *4 + 1); //create a random value for the suit values
setFaceName();
setSuitName();
}
/*Sets the string representation of the face using a
switch statement with cases for each numeric value between 1-13
*/
private void setFaceName()
{
switch (face)
{
case 1: faceName = "Ace"; break;
case 2: faceName = "Two"; break;
case 3: faceName = "Three"; break;
case 4: faceName = "Four"; break;
case 5: faceName = "Five"; break;
case 6: faceName = "Six"; break;
case 7: faceName = "Seven"; break;
case 8: faceName = "Eight"; break;
case 9: faceName = "Nine"; break;
case 10: faceName = "Ten"; break;
case 11: faceName = "Jack"; break;
case 12: faceName = "Queen"; break;
case 13: faceName = "King"; break;
}
}
//Sets the string representation of the suit using a switch statement with cases //for each nueric value 1-4
public void setSuitName ()
{
switch (suit)
{
case 1: suitName = "Clubs"; break;
case 2: suitName = "Diamonds"; break;
case 3: suitName = "Spades"; break;
case 4: suitName = "Hearts"; break;
}
}
//Returns the face (numeric value0 fo this card.
public int getFace ()
{
return face;
}
//Returns the suit (numeric value of this card)
public int getSuit()
{
return suit;
}
//Returns the string representation of this card, including both face and suit.
public String toString()
{
String result =faceName + " " + suitName;
return result;
}
}
Code:
import java.awt.*;
import javax.swing.*;
public class DealCards
{
// Creates and prints a random set of playing cards.
public static void main (String[] args)
{
// Create card objects c1, c2, c3, c4 and c5
Card c1,c2, c3, c4,c5;
c1 = new Card ();
c2 = new Card ();
c3 = new Card ();
c4 = new Card ();
c5 = new Card ();
// Display each of the card objects, its face value and suit value
System.out.println (c1 + " - (" + c1.getFace() + ", " + c1.getSuit() + ")");
System.out.println (c2 + " - (" + c2.getFace() + ", " + c2.getSuit() + ")");
System.out.println (c3 + " - (" + c3.getFace() + ", " + c3.getSuit() + ")");
System.out.println (c4 + " - (" + c4.getFace() + ", " + c4.getSuit() + ")");
System.out.println (c5 + " - (" + c5.getFace() + ", " + c5.getSuit() + ")");
}
}
Comment