Interface XI
Home Up Interface I Interface II Interface III Interface IV Interface V Interface VI Interface VII Interface VIII Interface IX Interface X Interface XI Interface XII Code

 

In today's lesson, we will continue examining the Interface.java code.  Specifically, we will examine the "paint", "trumpIsKnown", "playSequence", "mousePressed", "itemStateChanged", and "actionPerformed" methods.

The "paint" method takes care of drawing text and graphics on the display window.  It starts by dividing the display window up into three rows (two horizontal lines) and ten columns (nine vertical lines).  It then calls the "displayStrings" method several times in order to place appropriate text messages in designated locations.  A "for" loop displays the cards that are left in the player's hand.  Finally, the player's last card, the opponent's last card, and the card that is currently turned over are all displayed.  These last three cards can either be actual cards (e.g. the six of diamonds) or the pattern on the back of the cards, depending on the current situation.

The "trumpIsKnown" method is called when a trump suit has been named by one of the two players.  After trump is called, each player is dealt an additional three cards, each hand is scored for bonuses (e.g. four of a kind), the "startPlay" method is called to initialize some variables, the leader of the first trick is determined, and then play is begun.

The "playSequence" method takes care of one trick.  It begins by setting each card in the player's hand to be displayable.  The current card for both the player and the opponent are set to be the card back pattern (i.e. no cards have been played for this trick).  If the number of cards is equal to zero, the current hand is finished.  If the opponent gets to lead, the player must wait for the opponent to lead a card.  If the player gets to lead, the player must select one of her cards to lead.

The "mousePressed" method is a mouse event handling method that determines which card the player has selected to play.  The method commences by determining the "x" and "y" coordinates of the mouse click.  The "for" loop goes through each card in the display.  The "if" statement tests whether the mouse click occurred in the current image and tests whether the image is currently clickable (i.e. it is the player's turn to do something).  The next "if" statement makes sure that either it is the player's turn to lead (i.e. the player can play anything) or that if the computer led, the player is playing a legal card.  The body of the second "if" statement takes care of the logistics of processing the card played.  Do you understand what is happening?

The "mouseClicked", "mouseReleased", "mouseEntered", and "mouseExited" methods are empty, but are required to implement a "MouseListener".  Refer to the event handling summary for more information.

The "itemStateChanged" method is used to implement an "ItemListener" interface.  When the user selects choices from "Choice" menus, the "itemStateChanged" method is called.

The method body is only processed if a "Choice" item has been "SELECTED".  If it has, the label of the item is tested.  If the label is "Accept Trump", trump is called by the player based on the suit of the turned over card.  If the label is "Dont Accept", the suit of the flipped card is denied as the trump suit by the player.  If the label is "Clubs", "Diamonds", "Hearts", "Spades", "Nothing", or "Everything", the appropriate suit is called as trump by the player.   Finally, if the label is "Pass", the player does not want to call anything trump.  Take a look at the specifics of each of these four cases.  Do you understand what the code is doing?

The "actionPerformed" method is used to implement the "ActionListener" interface.  Anytime a "Button" is clicked, the "actionPerformed" method is called. 

The method calls the "getActionCommand" to determine the label of the button selected.  An "if", "else if" construct is used to take the appropriate action.  If the "Join" button is clicked, the total points of each player is set to zero, each player's hand is reset, the "Join" button is hidden, the "Rules" button is hidden, the "Wait1" button is displayed, and the messages are changed to reflect the current situation.

If the "Rules" button is clicked, the rules of Color Belot are displayed by going to the appropriate URL.  Do you want to learn about networking?

If the "Wait1" button is clicked, the dealer is selected, the "Wait1" button is hidden, the "Wait2" button is displayed, and the messages are updated to reflect the current situation.

Take a look at what happens when the "Wait2", "Wait3", "Wait4", "Wait5", "Wait6", "Wait7", and "Wait8" buttons are selected.  Do you understand the code in each case?   It might be illustrative to draw a diagram showing how the display window is updated as a hand of Color Belot is played.

At the bottom of the method, "repaint" is called so that the display window can be updated no matter which button was selected.