/** * Class: PokerChipsApplet * File: PokerChipsApplet.java * Original Author: Dave Peixotto * Creation Date: 1.25.2001 * Modified by: NULL * Last Modified: 1.25.2001 ************************************************************** * Class Description: This class is the applet that displays all of the * graphical data. The class has several piles of poker chips. There * are three piles for the user and three piles in the center that are * for the computer. Each pile has a different color of poker chip. The * User's piles each have an ante button below them that when pressed moves * the top most chip to the appropriate pile in the center. In addition * every pile has the total value of the pile displayed below it. There * is also two additional buttons: Computer wins and I win. When the * computer wins button is pressed the chips in the middle piles dissapear. * when the I win button is pressed the chips in the middle pile are moved * to the appropriate pile in the users section. * *************************************************************** * Instance Variables: * userGreenPile * userRedPile * userBluePile * antePile *** * * anteButtonPanel * actionButtonPanel */ import java.awt.*; import java.applet.*; import javax.swing.*; import java.awt.event.*; public class PokerChipsApplet extends JApplet implements ActionListener { //Class Variables*********************************** public static final int RED_CHIP_VALUE = 100; public static final int BLUE_CHIP_VALUE = 500; public static final int APPLET_WIDTH = 400; public static final int APPLET_HEIGHT = 400; public static final Color RED = Color.red; public static final Color BLUE = Color.blue; public static final Color GREEN = Color.green; private static final int BUTTON_HSPACING = 60; private static final int BUTTON_VSPACING = 5; public static final int PILE_SPACE_FROM_BOTTOM = 85; //Instance Variables******************************** //Piles Pile userRedPile; Pile userBluePile; Pile userGreenPile; Pile antePile = new Pile(0, RED, 0); // create pile with no chips //Panels JPanel anteButtonPanel = new JPanel(), actionButtonPanel = new JPanel(); //Buttons JButton anteRed = new JButton("Ante"), anteBlue = new JButton("Ante"), anteGreen = new JButton("Ante"), iWin = new JButton("I Win"), compWins = new JButton("Computer Wins"); //Dimensions Dimension appletDimensions; // used for positioning of various elements in the applet /**init()**** * This method initializes all of the panels and adds the buttons to the * appropriate panels */ public void init() { //set the size of the applet setSize(APPLET_WIDTH, APPLET_HEIGHT); //create the new piles reading the number of chips from the html file userRedPile = new Pile(Integer.parseInt(getParameter("nRedChips")), RED, RED_CHIP_VALUE); userBluePile = new Pile(Integer.parseInt(getParameter("nBlueChips")), BLUE, BLUE_CHIP_VALUE); userGreenPile = new Pile(Integer.parseInt(getParameter("nGreenChips")), GREEN, 0); //setup panels anteButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, BUTTON_HSPACING, BUTTON_VSPACING)); actionButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, BUTTON_HSPACING, BUTTON_VSPACING)); //initalize buttons initButtons(); //add panels getContentPane().add(actionButtonPanel, "North"); getContentPane().add(anteButtonPanel, "South"); repaint(); } /**paint()**** * this method has all the piles display themselves. The user piles use the buttons as * refrences as to where they should be displayed. The ante pile simply displays itself * in the center of the applet. *----------NOTE--------------- * The method calls to display the piles and their values are rather messy. This is * because their position is calculated on the fly using several landmarks to get the * correct positioning. As mentioned above the Ante pile is positioned in the middle of * the applet. The value of the ante pile is centered directly below the ante pile. The * Red green and blue piles are centered above their respective buttons and placed a * predetermined height above the bottom of the applet. Their values are placed directly * below the respective piles and centered beneath the pile. An attempt was made for this * applet to be able to respond to resizing reasonably well. As a result the method calls * are a bit ugly but I could not think of a better way to do it. 8( */ public void paint( Graphics g ) { super.paint(g); //call to repaint all the components in the applet //store the applets dimensions appletDimensions = new Dimension(getWidth(), getHeight()); //convert Graphics to Graphics2D Graphics2D g2 = (Graphics2D) g; //font metrics used to center textbeneath the piles FontMetrics metrics = g2.getFontMetrics(); //display all the piles---------------------------------------- //display antePile in middle of applet antePile.displayPile(g2, (int)((appletDimensions.getWidth() - PokerChip.X_SIZE)/ 2), (int)(appletDimensions.getHeight() / 2)); //display the antePile value if(antePile.getNumberOfChips() > 0 ) { g2.setPaint(Color.blue); g2.drawString("Value " + antePile.getPileValueString(), (int)((appletDimensions.getWidth() - PokerChip.X_SIZE)/ 2), (int)(appletDimensions.getHeight() / 2) + PokerChip.Y_SIZE + metrics.getHeight()); } //put redPile above red ante button userRedPile.displayPile(g2, anteRed.getX() + (anteRed.getWidth() - PokerChip.X_SIZE)/2, (int)(appletDimensions.getHeight() - PILE_SPACE_FROM_BOTTOM)); //display the value of the red pile g2.setPaint(Color.blue); g2.drawString("Value " + userRedPile.getPileValueString(), anteRed.getX() + ((anteRed.getWidth() - metrics.stringWidth("Value: " + userRedPile.getPileValueString()))/ 2), (int)(appletDimensions.getHeight() - PILE_SPACE_FROM_BOTTOM + PokerChip.Y_SIZE + metrics.getHeight() )); //put bluePile above blue ante button userBluePile.displayPile(g2, anteBlue.getX() + (anteBlue.getWidth() - PokerChip.X_SIZE)/2, (int)(appletDimensions.getHeight() - PILE_SPACE_FROM_BOTTOM)); //display the value of the blue pile g2.setPaint(Color.blue); g2.drawString("Value " + userBluePile.getPileValueString(), anteBlue.getX() + ((anteBlue.getWidth() - metrics.stringWidth("Value: " + userRedPile.getPileValueString() ))/ 2), (int)(appletDimensions.getHeight() - PILE_SPACE_FROM_BOTTOM + PokerChip.Y_SIZE + metrics.getHeight())); //put greenPile above green ante button userGreenPile.displayPile(g2, anteGreen.getX() + (anteGreen.getWidth() - PokerChip.X_SIZE)/2, (int)(appletDimensions.getHeight() - PILE_SPACE_FROM_BOTTOM)); //display the value of the green pile g2.setPaint(Color.blue); g2.drawString("Value " + userGreenPile.getPileValueString(), anteGreen.getX() + ((anteGreen.getWidth() - metrics.stringWidth("Value: " + userRedPile.getPileValueString()))/ 2), (int)(appletDimensions.getHeight() - PILE_SPACE_FROM_BOTTOM + PokerChip.Y_SIZE + metrics.getHeight() )); } /**initButtons()**** * method adds action listeners to all the buttons and then adds them to the appropriate panels */ private void initButtons() { //add actionlisteners anteRed.addActionListener(this); anteBlue.addActionListener(this); anteGreen.addActionListener(this); iWin.addActionListener(this); compWins.addActionListener(this); //add buttons to the correct panels anteButtonPanel.add(anteRed); anteButtonPanel.add(anteBlue); anteButtonPanel.add(anteGreen); actionButtonPanel.add(iWin); actionButtonPanel.add(compWins); } /**actionPerformed()**** * This method handles the pressing of the buttons. When an ante button is pressed the * chip is removed from the appropriate pile and placed in the ante pile. When the * "I win" button is pressed all of the chips in the ante pile are placed back into * their appropriate piles. When the computer wins button is pressed all of the chips * are removed from the ante pile and discarded. When anteing(sp?) a chip I simply call * the antepile addchip method andd pass it the chip that was removed from the appropriate * pile. I always remove the top chip from the pile which is calculated by the number of * chips in the pile -1 (to work with the indexing). *** * When I win is pressed. I loop through all of the chips in the pile and test their color * to place it back in the correct pile. I use a temporary poker chip to store the chip as * it is removed from the ante pile. This makes the program a little more readable. */ public void actionPerformed(ActionEvent e) { //Ante a red chip if(e.getSource() == anteRed) { if(userRedPile.getNumberOfChips() > 0) { antePile.addChip(userRedPile.removeChip(userRedPile.getNumberOfChips() - 1) ); repaint(); } } //Ante a blue chip else if(e.getSource() == anteBlue) { if(userBluePile.getNumberOfChips() > 0) { antePile.addChip(userBluePile.removeChip(userBluePile.getNumberOfChips() - 1) ); repaint(); } } //Ante a green chip else if(e.getSource() == anteGreen) { if(userGreenPile.getNumberOfChips() > 0) { antePile.addChip(userGreenPile.removeChip(userGreenPile.getNumberOfChips() - 1) ); repaint(); } } //I win, put the chips back in the proper pile else if(e.getSource() == iWin) { if(antePile.getNumberOfChips() > 0) { //create temporary chip PokerChip tmpChip = new PokerChip(Color.black,0); for(int i = antePile.getNumberOfChips() - 1; i >= 0; --i) { tmpChip = antePile.removeChip(i); //now test to see what color the chip is if(tmpChip.getColor().equals(RED) ) userRedPile.addChip(tmpChip); else if(tmpChip.getColor().equals(BLUE) ) userBluePile.addChip(tmpChip); else if(tmpChip.getColor().equals(GREEN) ) userGreenPile.addChip(tmpChip); } repaint(); } } //computer wins, remove all chips from ante pile else if(e.getSource() == compWins) { if(antePile.getNumberOfChips() > 0) { antePile.removeAllChips(); repaint(); } } }//actionPerformed() }//PokerChipsApplet()