import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.*;
public class BlackJackPanel extends JPanel
{
// declare data
private int bets;
private int cash = 0;
private JLabel cashLabel, DcardLabel, UcardLabel, moneyLabel, spaceLabel, winLabel, lossLabel;
private JButton deal, hit, stay, newDeck;
private int face;
private int suit;
private String faceName;
private String suitName;
private int game;
private int win;
private int lose;
private int dealerTot;
private int yourTot;
private final int BLACKJACK = 21;
private int value;
private int valueTot;
// create a Scanner object called scan for keyboard input
static Scanner scan = new Scanner(System.in);
// create radio buttons, labels, text areas, scroll panes
// listeners, groups and panels
JRadioButton fiveB, tenB, twentyFiveB, fiftyB, hundredB;
ButtonListener listen;
ButtonGroup group;
JPanel panel, centerPanel, eastPanel, westPanel;
// create places for the Card objects, imageicons and JLabels
Card c1, c2, c3, c4, c5 ;
ImageIcon iconc1, iconc2, iconc3, iconc4;
JLabel label1,label2, label3, label4, label5;
public BlackJackPanel()
{
// create the radio button group
group = new ButtonGroup();
// create the labels
DcardLabel = new JLabel ("Dealer Cards:" + valueTot);
UcardLabel = new JLabel ("Your Cards: " + yourTot);
cashLabel = new JLabel ("Cash:");
moneyLabel = new JLabel ("" + cash);
spaceLabel = new JLabel ("");
winLabel = new JLabel ("Wins: "+ win);
lossLabel = new JLabel (" Loss: " + lose);
// create each card instance or object
c1 = new Card(); // dealer
c2 = new Card(); // player
c3 = new Card(); // dealer
c4 = new Card(); // player
// create icons for each image using the getCardName () method
iconc1 = new ImageIcon(c1.getCardName());
iconc2 = new ImageIcon(c2.getCardName());
iconc3 = new ImageIcon(c3.getCardName());
iconc4 = new ImageIcon(c4.getCardName());
// Create labels for each card
label1 = new JLabel (c1.toString(), iconc1,SwingConstants.CENTER);
label2 = new JLabel (c2.toString(), iconc2,SwingConstants.CENTER);
label5 = new JLabel ();
label3 = new JLabel (c3.toString(), iconc3,SwingConstants.CENTER);
label4 = new JLabel (c4.toString(), iconc4,SwingConstants.CENTER);
// Create a panel, set the background color, set the size and add the panels
JPanel primary = new JPanel();
JPanel panel1 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel7 = new JPanel();
JPanel panel2 = new JPanel();
// Sets Background
primary.setBackground (Color.green );
panel1.setBackground (Color.red );
panel5.setBackground (Color.green );
panel7.setBackground (Color.green );
panel2.setBackground (Color.yellow );
// Sets pefered Size
primary.setPreferredSize (new Dimension(1000, 650));
panel1.setPreferredSize (new Dimension(800, 200 ));
panel5.setPreferredSize (new Dimension(800, 100 ));
panel7.setPreferredSize (new Dimension(800, 125 ));
panel2.setPreferredSize (new Dimension(800, 200 ));
// Adds panel to labels
panel1.add (label1);
panel1.add (label2);
panel5.add (label5);
panel5.add (winLabel);
panel7.add (lossLabel);
panel2.add (label3);
panel2.add (label4);
// Sets panel 5 layout
panel5.setLayout(new GridLayout (1, 0));
// Add panels to primary
primary.add (panel1);
primary.add (panel5);
primary.add (panel7);
primary.add (panel2);
// Sets position of card and name
label1.setHorizontalTextPosition (SwingConstants.CENTER);
label1.setVerticalTextPosition (SwingConstants.BOTTOM);
label2.setHorizontalTextPosition (SwingConstants.CENTER);
label2.setVerticalTextPosition (SwingConstants.BOTTOM);
label3.setHorizontalTextPosition (SwingConstants.CENTER);
label3.setVerticalTextPosition (SwingConstants.BOTTOM);
label4.setHorizontalTextPosition (SwingConstants.CENTER);
label4.setVerticalTextPosition (SwingConstants.BOTTOM);
ButtonListener listener = new ButtonListener();
// Creates buttons and labels for them
deal = new JButton ("Deal");
deal.addActionListener (new ButtonListener());
hit = new JButton ("Hit");
hit.addActionListener (new ButtonListener());
stay = new JButton ("Stay");
stay.addActionListener (new ButtonListener());
newDeck = new JButton ("New Deck");
newDeck.addActionListener (new ButtonListener());
// create main panel, color, layout and dimensions
panel = new JPanel();
panel.setBackground (Color.green);
panel.setPreferredSize(new Dimension (1000, 650));
panel.setLayout(new BorderLayout());
// create the center panel, color and JScrollPanel(ta)
centerPanel = new JPanel();
centerPanel.setBackground(Color.green);
centerPanel.add (panel1);
centerPanel.add (panel5);
centerPanel.add (panel7);
centerPanel.add (panel2);
// create the east panel with color
eastPanel = new JPanel();
eastPanel.setBackground (Color.lightGray);
eastPanel.setLayout(new GridLayout (6, 3, 5, 50));
eastPanel.add (spaceLabel);
eastPanel.add (deal);
eastPanel.add (hit);
eastPanel.add (stay);
eastPanel.add (newDeck);
// create the west panel with color and Box layout
westPanel = new JPanel();
westPanel.setBackground (Color.lightGray);
westPanel.setLayout (new BoxLayout (westPanel, BoxLayout.Y_AXIS));
westPanel.add (DcardLabel);
westPanel.add (UcardLabel);
westPanel.add (Box.createRigidArea (new Dimension (0, 10)));
// create the radio button group listener
listen = new ButtonListener();
// create all the radio buttons, including listener, color and group
fiveB = new JRadioButton ("5");
fiveB.addActionListener(listen);
fiveB.setBackground (Color.lightGray);
group.add(fiveB);
tenB = new JRadioButton ("10");
tenB.addActionListener(listen);
tenB.setBackground (Color.lightGray);
group.add(tenB);
twentyFiveB = new JRadioButton ("25");
twentyFiveB.addActionListener(listen);
twentyFiveB.setBackground (Color.lightGray);
group.add(twentyFiveB);
fiftyB = new JRadioButton ("50");
fiftyB.addActionListener(listen);
fiftyB.setBackground (Color.lightGray);
group.add(fiftyB);
hundredB = new JRadioButton ("100");
hundredB.addActionListener(listen);
hundredB.setBackground (Color.lightGray);
group.add(hundredB);
// place radio button group in west panel
westPanel.add (Box.createRigidArea (new Dimension (10, 450)));
westPanel.add (cashLabel);
westPanel.add (moneyLabel);
westPanel.add(fiveB);
westPanel.add(tenB);
westPanel.add(twentyFiveB);
westPanel.add(fiftyB);
westPanel.add(hundredB);
// place all panels in the Border Layout positions
panel.add(centerPanel, BorderLayout.CENTER);
panel.add(westPanel, BorderLayout.WEST);
panel.add(eastPanel, BorderLayout.EAST);
// add the main panel
add (panel);
if (valueTot == BLACKJACK)
{
game = win;
}
else
{
game = lose;
}
}
//Button listeners and Actions Perferomed
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// Attempt for button actions
if (event.getSource() == deal)
{
}
// If the game is won or lost
if (dealerTot == BLACKJACK || yourTot == BLACKJACK)
{
cash = 0;
}
else if (dealerTot == BLACKJACK)
{
game = lose;
}
else if (yourTot == BLACKJACK)
{
game = win;
}
// check for which radio button was clicked
// is event fiveB
if (event.getSource() == fiveB)
{
// bets = 5 dollors
if (game == win)
{
cash =+ 5;
}
else
{
cash =- 5;
}
}
// else is event tenB
else if (event.getSource() == tenB)
{
// bets = 10 dollors
if (game == win)
{
bets =+ 10;
}
else
{
bets =- 10;
}
}
// else is event twentyFiveB
else if (event.getSource() == twentyFiveB)
{
// bets = 25 dollors
if (game == win)
{
bets =+ 25;
}
else
{
bets =- 25;
}
}
// else is event fiftyB
else if (event.getSource() == fiftyB)
{
// bets = 50 dollors
if (game == win)
{
bets =+ 50;
}
else
{
bets =- 50;
}
}
// else is event hundredB
else if (event.getSource() == hundredB)
{
// bets = 100 dollors
if (game == win)
{
bets =+ 100;
}
else
{
bets =- 100;
}
}
cash = bets;
}
public void Card ()
{
// Create a random value for the face and suit values
face = (int) (Math.random() * 13 + 1);
suit = (int) (Math.random() * 4 + 1);
// Create the names for the face and suit of the card
setFaceName();
setSuitName();
}
//-----------------------------------------------------------------
// Sets the string representation of the face using a switch statement
// with cases for each numeric value 1-13.
//-----------------------------------------------------------------
private void setFaceName()
{
switch (face)
{
case 1: value = 1; faceName = "Ace of";
break;
case 2: value = 2; faceName = "Two of" ;
break;
case 3: value = 3; faceName = "Three of";
break;
case 4: value = 4; faceName = "Four of" ;
break;
case 5: value = 5; faceName = "Five of" ;
break;
case 6: value = 6; faceName = "Six of" ;
break;
case 7: value = 7; faceName = "Seven of";
break;
case 8: value = 8; faceName = "Eight of";
break;
case 9: value = 9; faceName = "Nine of" ;
break;
case 10: value = 10; faceName = "Ten of" ;
break;
case 11: value = 11; faceName = "Jack of" ;
break;
case 12: value = 12; faceName = "Queen of";
break;
case 13: value = 13; faceName = "King of" ;
break;
}
}
//-----------------------------------------------------------------
// Sets the string representation of the suit using a switch statement
// with cases for each numeric value 1-4.
//-----------------------------------------------------------------
private void setSuitName()
{
switch (suit)
{
case 1: suitName = " Clubs" ; break;
case 2: suitName = " Diamonds"; break;
case 3: suitName = " Hearts" ; break;
case 4: suitName = " Spades" ; break;
}
}
//-----------------------------------------------------------------
// Returns the face (numeric value) of 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 ()
{
return ( faceName + suitName);
}
//-----------------------------------------------------------------
// Returns the name of the card image
//-----------------------------------------------------------------
public String getCardName ()
{
return "Cards/" + faceName + suitName +".gif";
}
}
}