Java Programming: From the Ground Up

Slides:



Advertisements
Similar presentations
Dealer Comm Hand Player makes Ante bet and optional Bonus bet. Five cards are dealt to each player from the shuffler. Five cards are dealt from the shuffler.
Advertisements

Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Intro to Probability & Games
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Lecture 19 Graphics User Interfaces (GUIs)
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
F27SB2 Software Development 2 Lecture 6: Java GUIs 5.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
CS 4244: Internet Programming User Interface Programming in Java 1.0.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Object-Oriented Programming (Java), Unit 28 Kirk Scott 1.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Creating a GUI Class An example of class design using inheritance and interfaces.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Lecture 15 Basic GUI programming
Modular Event Handling
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
Swing JComponents.
A First Look at GUI Applications
GUIs Model/View/Controller Layouts
Java Programming: From the Ground Up
Chapter 5 Black Jack.
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Event-driven programming for GUI
PC02 Term 1 Project Basic Messenger. PC02 Term 1 Project Basic Messenger.
Timer class and inner classes
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Task 2 Implementation help
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Java Programming: From the Ground Up Chapter 20 A Case Study: Video Poker, Revisited

Objective The objective of this chapter is an understanding of the design principle that entails the separation of the data model from the interface, or more simply, the model from the view. He we replace the text-based user interface developed in Chapter 11 with a more visual GUI that utilizes buttons, labels, and pictures. The separation of model from view that was underscored in the case study of Chapter 11 enables us to plug in a new graphical interface with minimal effort.  

A Quick Review The poker application of Chapter 11 consists of seven interacting classes: Player PokerGame Bet Deck Card Hand Bankroll

A Quick Review Class Actions Player PokerGame Bet Deck Attributes Bankroll bankroll; PokerGame pg; Bet bet; Player player Bet bet Bankroll bankroll Hand hand int bet Card deck[] Actions Initialize the bankroll. Add coins. Bet and Play. Discard. Display a Hand. Quit. Display final results. Present a menu. View initial hand. Discard or hold cards. Update bankroll. Get the bet. Set the bet. Shuffle the deck. Deal a card. Card Hand Bankroll int suit int value Card [] hand Deck deck int bankroll Get the suit. Get the value, i.e., rank. Get the name of a card. Evaluate the hand. Deal a new hand. Update a hand. Give the hand. Get the bankroll. Set the bankroll. Change the bankroll.

A Quick Review The Player class provides a text-based user interface. It is the Player class that we re-implement here, replacing text-based input and output with a GUI of buttons, labels, and pictures. Replacing the text-based UI of Chapter 11 with a GUI does not require knowledge of the implementation details of the other classes.

A Quick Review To replace the old user interface with a GUI, all that is needed is information about the objectives and methods of some of the classes. The next figures show those classes and methods that we use in creating a new GUI based poker game. Except for the Player class, which handles all input and output, no previously developed class needs alteration.

A Quick Review Class Purpose Constructor Method method Bankroll manages the number of coins in the machine Bankroll(); sets initial coin number to 0 void alterBankroll(int n); adds n coins to the number of coins in the machine int getBankRoll(); returns the number of coins currently in the machine

A Quick Review Class Purpose Constructor Method method Hand maintains a hand of five cards Hand(); creates an empty hand String[] getHand(); returns an array of five String references that describes a hand, e.g., {“Ace of Hearts”, “2 of Spades”, “3 of Diamonds’,…}

A Quick Review Class Purpose Constructor Method method Bet manages the current bet or wager Bet( int n); sets the bet to n coins int getBet(); returns the current bet void setBet(int n);

A Quick Review Class Purpose Constructor Method method PokerGame plays the game: deals and updates the hands, maintains the list of discarded cards PokerGame(Bet bet, Bankroll bankroll, Player player ); initializes the bet and bankroll for a player void viewInitialHand( ); requests a hand of five cards via hand.getHand() asks the player to display the hand via the message player.displayHand(hand) void discardOrHoldCards(); queries the player for the list of discarded cards: player.getDiscard(…); updates the hand; requests that the player display the new hand: player,displayHand() evaluates the hand; determines the winnings/losses; updates the bankroll; asks the player to display the results: player.displayResults()

A Visual Poker Game The GUI consists not of a text menu, but of buttons, labels, and images.

A Visual Poker Game A video poker GUI

A Visual Poker Game Before playing a hand of poker, a player must insert coins into the machine. This action is simulated by clicking the Add 1 or the Add 5 button. These buttons can be clicked repeatedly. Each time a player clicks one of these buttons, either 1 or 5 coins are “inserted” into the machine. The bottom panel of the GUI displays the current number of coins, that is, the bankroll.

A player inserts three coins A Visual Poker Game A player inserts three coins

A Visual Poker Game Once a player inserts coins into the machine, he clicks one of the five Bet buttons A bet from one to five coins is placed when clicked, but not more than the number of coins in the machine. A hand of five cards is then dealt.

A player bets two coins and a hand is dealt A Visual Poker Game A player bets two coins and a hand is dealt

A Visual Poker Game After the initial hand is dealt, a player has the option of keeping or discarding any of those five cards. To “hold” or keep a card, a player clicks the number that is displayed below the card and that number is replaced by the word Hold.

Two cards are marked Hold A Visual Poker Game Two cards are marked Hold

A Visual Poker Game A player then clicks the Deal button and those cards that the player chooses to discard are replaced with different cards. The hand is scored and the number of coins updated.

A Visual Poker Game The player keeps the two aces. The other three cards are replaced resulting in a hand containing two pair, which pays 2 to 1.

Laying Out the Frame First create a Player class that extends JFrame and includes the buttons and labels of the GUI. The following code builds a non-functioning GUI, i.e., a GUI with no listeners.

Laying Out the Frame /////////////// Player class, a GUI for video poker /////////////// public class Player extends JFrame { private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // add 1 coin private JButton add5Button // clicking adds 5 coins; private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking any of these buttons makes a bet and begins play

Laying Out the Frame public Player() // default constructor, places all components { super("Video Poker"); setBounds(0,0,400,500); // the label at the top of the frame resultLabel = new JLabel(); resultLabel.setFont(new Font("Arial", Font.BOLD, 18)); resultLabel.setText("Video poker"); //The five card images , the initial image is "Back.gif," which is a dummy card cardLabel = new JLabel[5]; for (int i = 0; i < 5; i++) cardLabel[i] = new JLabel(new ImageIcon("Back.gif"));  // the five hold/discard buttons holdButton = new JButton[5];

Laying Out the Frame for (int i = 0; i <5; i++) { holdButton[i] = new JButton(""+(i+1)); // initially these have numbers holdButton[i].setFont(new Font("Arial",Font.BOLD, 18)); holdButton[i].setEnabled(false); // initially turned off } // the five bet and play buttons betAndPlayButton = new JButton[5]; for ( int i = 0; i < 5; i++) betAndPlayButton[i] = new JButton("Bet "+(i+1)); betAndPlayButton[i].setEnabled(false); // initially turned off betAndPlayButton[i].setFont(new Font("Arial", Font.BOLD, 15));

Laying Out the Frame // the deal button, initially turned off dealButton = (new JButton("Deal")); dealButton.setFont(new Font("Arial", Font.BOLD, 18)); dealButton.setEnabled(false); // the quit button quitButton = new JButton("Quit"); quitButton.setFont(new Font("Arial", Font.BOLD, 15)); // label that displays current number of coins, the bankroll bankrollLabel = new JLabel(); bankrollLabel.setFont(new Font("Arial", Font.BOLD, 24)); bankrollLabel.setText("Coins remaining: " + 0 ); // initially no coins // two buttons that add 1 0r 5 coins to the machine add1Button = new JButton("Add 1"); add5Button = new JButton("Add 5"); add1Button.setFont(new Font("Arial", Font.BOLD, 15)); add5Button.setFont(new Font("Arial", Font.BOLD, 15));

Laying Out the Frame // panel for the play buttons, card labels, hold buttons, deposit buttons, deal and quit JPanel centerPanel = new JPanel(new GridLayout(4,5)); // add the five bet-and -play buttons for (int i = 0; i < 5; i++) centerPanel.add(betAndPlayButton[i]);  //add the five labels that displays the card images centerPanel.add(cardLabel[i]); // add the five hold buttons centerPanel.add(holdButton[i]);

Laying Out the Frame // add the two deposit buttons, the a blank button, the deal and quit buttons centerPanel.add(add1Button); centerPanel.add(add5Button); centerPanel.add(new JButton()); // a blank button as a separator centerPanel.add(dealButton); centerPanel.add(quitButton); //add the label that displays the results to the NORTH section of the frame add(resultLabel, BorderLayout.NORTH); // add the label that displays the coin count to the SOUTH section of the frame add(bankrollLabel, BorderLayout.SOUTH); // add the panel with the buttons and card labels to the CENTER of the frame add(centerPanel, BorderLayout.CENTER); setResizable(false); setVisible(true); }

Adding Coins Before a hand of poker can be played, a player must deposit coins into the machine. This is accomplished by clicking the Add 1 button or Add 5 button. Each click increases the bankroll by either one or five coins. Once coins have been added, the appropriate Bet buttons are enabled. For example, if a player deposits three coins, the buttons labeled Bet 1, Bet 2, and Bet 3 are enabled but Bet 4 and Bet 5 are not. The Bet 4 and Bet 5 buttons are disabled because you cannot bet four or more coins when there are just three coins in the machine! If a player deposits seven coins, then all five buttons are enabled. A Bankroll object manages the number of coins deposited into the machine.

Adding Coins Clicking Add 1 or Add 5 generates an action event that we handle with an inner class called ButtonHandler. This listener handles all events from either button. The following code: declares and initializes a Bankroll reference, bankroll, and implements ButtonListener, an inner class that responds to events generated by add1Button and add5Button. The response of ButtonListener necessitates: incrementing the bankroll, displaying the number of coins in the machine on the label referenced by bankrollLabel, and enabling the appropriate Bet and Play buttons.

Adding Coins public class Player extends JFrame { private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // clicking adds 1 coin private JButton add5Button; // clicking adds 5 coins private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking buttons makes a bet and //begins play private Bankroll bankroll; // manages the number of coins in the machine

Adding Coins public Player() // constructor, places all components, registers listeners { // as above bankroll = newBankroll(); add1Button.addActionListener(new ButtonListener()); //register listener add5Button.addActionListenet( new Button Listener()); // register listener } private class ButtonListener implements ActionListener// responds to button events public void actionPerformed( ActionEvent e) if ((e.getSource() == add1Button) || (e.getSource() == add5Button))

Adding Coins { if (e.getSource() == add1Button) bankroll.alterBankroll(1); // add one coin to the bankroll else bankroll.alterBankroll(5); // add 5 coins   int br = bankroll.getBankroll(); // total number of coins deposited bankrollLabel. setText("Coins remaining: "+ br ); // display total coins // enable the appropriate bet buttons for ( int i = 0; i < 5; i++) if (br >= (i+1)) betAndPlayButton[i].setEnabled(true); return; }

The First Hand After a player inserts coins, he/she is ready to play a hand of poker. Now, the player clicks one of the buttons labeled Bet 1, Bet 2,…, Bet 5. Clicking one of these buttons determines the current bet and deals the initial poker hand. To the Player class we add code that: registers the ButtonListener class with each of the five Bet buttons, and responds to the Bet button events: instantiates and sets the bet, displays the bet on the label referenced by resultLabel, instantiates a new PokerGame passing bet, bankroll, and player as parameters, displays images of the cards that make up the hand, enables the Hold and Deal buttons, and disables the Bet buttons, the Add 1 and Add 5 buttons, and the Quit button.

The First Hand public class Player extends JFrame { private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // add 1 coin private JButton add5Button // clicking adds 5 coins; private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking any of these buttons makes a bet and // begins play private Bankroll bankroll; // maintains number of coins in the machine private PokerGame pokerGame; private Bet bet; privateHand hand;

The First Hand public Player() // default constructor, lays out components, registers listeners { // as previously coded for (int i = 0; i< 5; i++) // register ButtonListener with each button betAndPlayButton[i].addActionListener(new ButtonListener()); } private class ButtonListener implements ActionListener public void actionPerformed( ActionEvent e) if ((e.getSource() == add1Button) || (e.getSource() == add5Button)) { // as previously coded }

The First Hand for (int i = 0; i < 5; i++) // respond to betAndPlayButton[i] if (e.getSource() == betAndPlayButton[i]) { bet = new Bet(); bet.setBet(i+1); // set the bet for this hand resultLabel.setText("Bet is "+(i+1)); // display the bet on the label pokerGame = new PokerGame(bet, bankroll,Player.this ); // instantiate PokerGame pokerGame.viewInitialHand(); // ask pokerGame to deal the first hand for (int j = 0; j < 5; j++) // for each hold button holdButton[j].setText(""+(j+1)); // display the card number holdButton[j].setEnabled(true); // enable the button }

The First Hand dealButton.setEnabled(true); // enable the deal button add1Button.setEnabled(false); // disable add1Button.. add5Button.setEnabled(false); // disable add5Button quitButton.setEnabled(false); // disable quitButton for (int j = 0; j <5; j++) // disable all betAndPlayButtons betAndPlayButton[j].setEnabled(false); return; }

The First Hand Notice that the response to a betAndPlayButton event includes sending a message to pokerGame (line 36): pokerGame.viewInitialHand() The viewInitialhand() method of PokerGame consists of two method calls: public void viewInitialHand() { hand.newHand(); player.displayHand(hand); } The call to newHand() creates a new hand of five cards. This method works correctly regardless of the interface. However, the second call is a Player method, displayHand(hand).

The First Hand The text-based version of Player implements displayHand(Hand hand) as: public void displayHand(Hand hand) { String [] handString = hand.getHand(); for(int i = 0; i < 5; i++) System.out.println((i+1) +". "+handString[i]); } That is, a hand is displayed on the screen as a list of strings: Ace of Hearts Queen of Clubs Queen of Hearts 3 of Spades 4 of Hearts

The First Hand To accomplish visual we use a collection of 52 card images, conveniently named Ace of Hearts.gif, Ace of Spades.gif ,…, 10 of Hearts.gif, 10 of Spades.gif, etc. Moreover, the Hand method: String[] getHand() returns an array of five strings, e.g. , {“Ace of Spades ”, “Queen of Clubs” , “Queen of Hearts”, “3 of Spades”, “4 of Hearts”}.

The First Hand A revised displayHand() for a revised Player class can be written as: public void displayHand(Hand hand) { String[] handString = hand.getHand(); for (int i = 0; i < 5; i++) String name = handString[i]+".gif"; // name is an image file name cardLabel[i].setIcon(new ImageIcon(name)); // display images on labels } Thus, in addition to the constructor and the inner class ButtonListener, the GUI version of Player, like the text version, implements the displayHand(Hand hand) method.

Holding Cards A player has the option of holding or discarding any or all of his/her five cards. To retain a card, a player presses the numbered button shown directly below the card. The response to pressing any one of these buttons changes the button’s text from a number to the string “Hold” and disables the button. To register a listener with each such button we add the following statement to the constructor: for (int i = 0; i < 5; i++) holdButton[i].addActionListener(new ButtonListener());

Holding Cards To respond to events generated by these buttons, we add code to ButtonListener that changes a button’s text to “Hold” and disables the button: for (int i = 0; i < 5; i++) if (e.getSource() == holdButton[i]) // source is button[i] { holdButton[i].setText("Hold"); holdButton[i].setEnabled(false); return; } Once a player clicks a Hold button, the button is disabled and the decision cannot be reversed. One could certainly add a mechanism that allows a player to change his/her mind, but we opt for simplicity.

The New Hand After a player decides which cards to hold and which to discard, he/she clicks the Deal button. This action generates an event. The response to this event: invokes pokerGame.discardOrHoldCards(), disables the Deal and Hold buttons, and enables the other buttons.

The New Hand In addition to registering ButtonHandler as a listener for dealButton, we add the following if statement to the ButtonHandler class to handle a Deal button event: if (e.getSource() == dealButton) { pokerGame.discardOrHoldCards(); // discardOrHoldCards() does the work // enable and disable the appropriate buttons dealButton.setEnabled(false); for(int j = 0; j < 5; j++) holdButton[j].setEnabled(false); for ( int i = 0; i < 5; i++) if (bankroll.getBankroll() >= (i+1)) betAndPlayButton[i].setEnabled(true); add1Button.setEnabled(true); add5Button.setEnabled(true); quitButton.setEnabled(true); }

The New Hand The PokerGame method discardOrHoldCards() defined in Chapter 11 manages the updated hand: public void discardOrHoldCards() { player.getDiscard(holdCards); hand.updateHand(holdCards); player.displayHand(hand); int payoff = hand.evaluateHand(); int winnings = updateBankroll(payoff); player.displayResults(payoff, winnings); }

The New Hand Notice that discardOrHoldCards() invokes three Player methods: void getDiscard(boolean[] holdCards), void displayHand(Hand hand), and void displayResults( int payoff, int winnings).

The New Hand The getDiscard(boolean[] holdCards) method of the text-based Player class sets holdCards[i] to true if the player opts to keep the ith card and false otherwise. That is, the Player method getDiscard(…) tells the caller which cards to keep and which to discard. The new GUI Player class must do likewise. When a player retains a card, the corresponding Hold button is disabled.

The New Hand getDiscard(boolean[] holdCards) can be implemented by checking whether or not a Hold button is enabled: public void getDiscard(boolean[] holdCards) { for ( int i = 0; i < 5; i++) // check whether or not the Hold button is enabled if (holdButton[i].isEnabled()) // button was not clicked holdCards[i] =false; else // button was clicked and enabled holdCards[i] = true; } The displayResults( int payoff, int winnings) remains to be implemented.

The New Hand The text-based version of Player implements this method as:   public void displayResults(int payoff, int winnings) { String nameOfHand = "Lose"; if (payoff == 250) nameOfHand = "Royal Flush"; else if (payoff == 50) nameOfHand = "Straight Flush"; else if (payoff == 25) nameOfHand = "Four of a Kind"; else if (payoff == 9) nameOfHand = "Full House"; else if (payoff == 6) nameOfHand = " Flush"; else if (payoff == 4) nameOfHand = "Straight "; else if (payoff == 3) nameOfHand = "Three of a Kind"; else if (payoff == 2) nameOfHand = "Two Pair"; else if (payoff == 1) nameOfHand = " Pair of Jacks or Better";

The New Hand { System.out.println("Winner: "+ nameOfHand); if (winnings >0 ) { System.out.println("Winner: "+ nameOfHand); System.out.println("Payoff is "+ winnings + " coins."); } else System.out.println("You lost your bet of "+ bet.getBet()); System.out.println("Current Bankroll is " + bankroll.getBankroll()); System.out.println(); This method can be incorporated into the new Player class with minimal change. The game’s outcome is displayed on two labels rather than in a text-based window using System.out.println().

The New Hand The only code that must be altered is the final if-else statement: // use a label rather than println() for output if (winnings >0 ) resultLabel.setText("Winner: "+ nameOfHand+" - pays "+ winnings); else resultLabel.setText("You lost your bet of "+ bet.getBet()); bankrollLabel.setText("Coins remaining: " + bankroll.getBankroll());

The Complete Player Class The new Player class has the following skeletal form that includes a constructor, three methods, and a private inner class: public class Player extends JFrame { // The Constructor public Player() sets up the components of the GUI registers listener with buttons }   // Three Methods public void displayHand(Hand hand) displays images of the five cards in hand

The Complete Player Class public void getDiscard(boolean[] holdCards) { holdCards[i] == true // if the ith card is retained }   public void displayResults(int payoff, int winnings) { displays the outcome of a hand // Listener – an Inner Class private class ButtonListener implements ActionListener public void ActionPerformed(ActionEvent e) responds to events generated by GUI buttons

The Complete Player Class The complete class, although rather lengthy, is direct and uncomplicated. The card images are assumed to be in a folder, Cards, which is in the same directory as the Player class.

The Complete Player Class import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Player extends JFrame { private JLabel resultLabel; // label displays the type of hand and the payout private JLabel[] cardLabel; // an array of 5 labels that display card images private JButton[] holdButton; // click to keep a particular card private JButton add1Button; // add 1 coin private JButton add5Button; // clicking adds 5 coins; private JLabel bankrollLabel; // label that displays the current number of coins private JButton quitButton; // exit the application private JButton dealButton; // click to display the updated hand private JButton[] betAndPlayButton; // clicking makes a bet and begins play private Bankroll bankroll; private PokerGame pokerGame; private Bet bet; private Hand hand;

The Complete Player Class public Player() // constructor { super("Video Poker"); bet = new Bet(); bankroll = new Bankroll(); setBounds(0,0,400,500); // the label places at the NORTH area of the frame resultLabel = new JLabel(); resultLabel.setFont(new Font("Arial", Font.BOLD, 18)); resultLabel.setText("Video poker"); // Display five card images , the initial image is "Back.gif" – a dummy card cardLabel = new JLabel[5]; for (int i = 0; i < 5; i++) cardLabel[i] = new JLabel(new ImageIcon("Cards/Back.gif"));

The Complete Player Class // the five hold/discard buttons holdButton = new JButton[5]; for (int i = 0; i <5; i++) { holdButton[i] = new JButton(""+(i+1)); // initially display numbers 1 - 5 holdButton[i].setFont(new Font("Arial",Font.BOLD, 18)); holdButton[i].setEnabled(false); // initially turned off } // the five "bet and play" buttons betAndPlayButton = new JButton[5]; for ( int i = 0; i < 5; i++) betAndPlayButton[i] = new JButton("Bet "+(i+1)); // display Bet 1, Bet 2,…Bet 5 betAndPlayButton[i].setEnabled(false); // initially turned off betAndPlayButton[i].setFont(new Font("Arial", Font.BOLD, 15));

The Complete Player Class // the deal button, initially turned off dealButton = (new JButton("Deal")); dealButton.setFont(new Font("Arial", Font.BOLD, 18)); dealButton.setEnabled(false); // the quit button quitButton = new JButton("Quit"); quitButton.setFont(new Font("Arial", Font.BOLD, 15)); // label that displays current number of coins, i.e. the "bankroll" bankrollLabel = new JLabel(); bankrollLabel.setFont(new Font("Arial", Font.BOLD, 24)); bankrollLabel.setText("Coins remaining: " + 0 ); // initially no coins // two buttons that either add 1 or 5 coins to the machine add1Button = new JButton("Add 1"); // displays "Add1" add5Button = new JButton("Add 5"); add1Button.setFont(new Font("Arial", Font.BOLD, 15)); add5Button.setFont(new Font("Arial", Font.BOLD, 15));

The Complete Player Class // panel holds bet-and-play buttons, card labels, hold buttons, deposit buttons, // deal and quit JPanel centerPanel = new JPanel(new GridLayout(4,5)); // add the five be t-and - play buttons for (int i = 0; i < 5; i++) centerPanel.add(betAndPlayButton[i]); //add the five labels that display the card images centerPanel.add(cardLabel[i]); // add the five hold buttons centerPanel.add(holdButton[i]); // add the two deposit buttons, the a blank button, the deal and quit buttons centerPanel.add(add1Button); centerPanel.add(add5Button); centerPanel.add(new JButton()); // a blank button as a separator centerPanel.add(dealButton); centerPanel.add(quitButton);

The Complete Player Class // add the label that displays the game results to the NORTH section of the frame add(resultLabel, BorderLayout.NORTH); // add the label that displays the coin count to the SOUTH section of the frame add(bankrollLabel, BorderLayout.SOUTH); // add the panel that holds the buttons and card labels to the CENTER of the frame add(centerPanel, BorderLayout.CENTER); //register listeners, one inner class does all listening add1Button.addActionListener(new ButtonListener()); add5Button.addActionListener(new ButtonListener()); dealButton.addActionListener(new ButtonListener()); quitButton.addActionListener(new ButtonListener()); for (int i = 0; i< 5; i++) betAndPlayButton[i].addActionListener(new ButtonListener());

The Complete Player Class for (int i = 0; i < 5; i++) holdButton[i].addActionListener(new ButtonListener()); setResizable(false); setVisible(true); } public void displayHand(Hand hand) // displays images of five cards { String[] handString = hand.getHand(); String name = "Cards/"+handString[i]+".gif"; // name is a file name. cardLabel[i].setIcon(new ImageIcon(name));

The Complete Player Class public void getDiscard(boolean[] holdCards) // maintains hold/discard information { for ( int i = 0; i < 5; i++) if (holdButton[i].isEnabled()) // card is discarded holdCards[i] =false; else // card is retained holdCards[i] = true; } public void displayResults(int payoff, int winnings) // displays the final outcome l String nameOfHand = "Lose"; if (payoff == 250) nameOfHand = "Royal Flush"; else if (payoff == 50) nameOfHand = "Straight Flush"; else if (payoff == 25) nameOfHand = "Four of a Kind";

The Complete Player Class else if (payoff == 9) nameOfHand = "Full House"; else if (payoff == 6) nameOfHand = " Flush"; else if (payoff == 4) nameOfHand = "Straight "; else if (payoff == 3) nameOfHand = "Three of a Kind"; else if (payoff == 2) nameOfHand = "Two Pair"; else if (payoff == 1) nameOfHand = " Pair of Jacks or Better"; if (winnings >0 ) // display outcome on resultLabel resultLabel.setText("Winner: "+ nameOfHand+" - pays "+ winnings); else resultLabel.setText("You lost your bet of "+ bet.getBet()); bankrollLabel.setText("Coins remaining: " + bankroll.getBankroll()); }

The Complete Player Class private class ButtonListener implements ActionListener // respond to button events { public void actionPerformed( ActionEvent e) if ((e.getSource() == add1Button) || (e.getSource() == add5Button)) // click Add 1/ Add 5 if (e.getSource() == add1Button) bankroll.alterBankroll(1); else bankroll.alterBankroll(5); int br = bankroll.getBankroll(); bankrollLabel. setText("Coins remaining: "+ br ); for ( int i = 0; i < 5; i++) if (br >= (i+1)) betAndPlayButton[i].setEnabled(true); return; }

The Complete Player Class if (e.getSource() == quitButton) // click the Quit button System.exit(0); for (int i = 0; i < 5; i++) // click one of the five bet-and-play buttons if (e.getSource() == betAndPlayButton[i]) { bet = new Bet(); bet.setBet(i+1); resultLabel.setText("Bet is "+(i+1)); pokerGame = new PokerGame(bet, bankroll ,Player.this ); pokerGame.viewInitialHand(); for(int j = 0; j < 5; j++) // enable the hold buttons holdButton[j].setText(""+(j+1)); holdButton[j].setEnabled(true); }

The Complete Player Class // enable and disable other buttons add1Button.setEnabled(false); add5Button.setEnabled(false); quitButton.setEnabled(false); dealButton.setEnabled(true); for (int j = 0; j <5; j++) betAndPlayButton[j].setEnabled(false); return; } for (int i = 0; i < 5; i++) // respond to a Hold button event if (e.getSource() == holdButton[i]) { holdButton[i].setText("Hold"); holdButton[i].setEnabled(false);

The Complete Player Class if (e.getSource() == dealButton) // respond to a Deal button event { pokerGame.discardOrHoldCards(); dealButton.setEnabled(false); for(int j = 0; j < 5; j++) holdButton[j].setEnabled(false); for ( int i = 0; i < 5; i++) if (bankroll.getBankroll() >= (i+1)) // enough coins ? betAndPlayButton[i].setEnabled(true); add1Button.setEnabled(true); add5Button.setEnabled(true); quitButton.setEnabled(true); } public static void main(String[] args) Player pm = new Player();