/** * File: Pile.java * Author: Robert Banagale * Last Modified: March 2001 * Description: Class that contains information and methods needed by a pile of Pokerchips * * */ import java.awt.*; import javax.swing.*; public class Pile extends JPanel { private static final int RED = 1; // Chip Color Identifiers. private static final int BLUE = 2; private static final int GREEN = 3; private static final int EMPTY = 0; // Non-existant chip. private static final int ZERO_LENGTH = 0; private static final int RED_VALUE = 100; // Chip Value Constants private static final int BLUE_VALUE = 500; private static final int ZERO_VALUE = 0; // Non-existant chip. private static final int MYPILE = 0; // Pile Type Identifiers private static final int MYANTEPILE = 1; private static final int COMPANTEPILE = 2; private static final int COMPUTERPILE = 3; private static final int CHIPWIDTH = 50; // Standard Dimensions of a Poker Chip private static final int CHIPHEIGHT = 50; public boolean pileSelected = false; // UI value, is the pile selected. public boolean pileHot = false; // UI value, is the pile 'under mouse' public int pileID = 0; // Pile Identifier public int pileLength = 0; // Total length of pile public PokerChip[] pilePokerChip; // Array of PokerChips public void init(Graphics g) // Refresh pile { repaint(); } // init() public void initPile(int passedColor) // Initlize passed in Pile { try { if ( ((this.getPileID()) != (MYANTEPILE)) && ((this.getPileID()) != (COMPANTEPILE))) this.pilePokerChip = new PokerChip[(this.getPileLength() * 2)]; // Create Array with specific Length else this.pilePokerChip = new PokerChip[(this.getPileLength())]; // Create Array with specific Length for (int i = 0; i < (this.pilePokerChip.length); i++) // Set the color of each chip this.pilePokerChip[i] = new PokerChip(); for (int j = 0; j < (this.getPileLength()); j++) // Set the Value of each chip { this.pilePokerChip[j].setChipColor(passedColor); if (this.pilePokerChip[j].chipColor == RED) // Assign Red chip's value to 100 this.pilePokerChip[j].setChipValue(RED_VALUE); if (this.pilePokerChip[j].chipColor == BLUE) // Assign Blue chip's value to 500 this.pilePokerChip[j].setChipValue(BLUE_VALUE); if (this.pilePokerChip[j].chipColor == GREEN) // Assign Green chip's random value { int randGreenValue = 0; // Temporary int value of a Green chip Long longRandGreenValue = new Long(Math.round((Math.random() * 1000))); // Calculate a random value between 1 and 1000 randGreenValue = longRandGreenValue.intValue(); // Temporary Long value of a Green chip this.pilePokerChip[j].setChipValue(randGreenValue); // Cast Long to int; } // if if (this.pilePokerChip[j].chipColor == EMPTY) this.pilePokerChip[j].setChipValue(ZERO_VALUE); } // for loop } // try catch (NullPointerException nullPointerProblem) { // Watch for an attempt to reference a null object. System.out.println("ERROR: NullPointerException in InitPile Method"); } // Catch IOOBE } // initPile() public void setPileSelected(boolean passedSetting) // Sets a Pile to 'selected' state. { this.pileSelected = passedSetting; // Set State this.repaint(); // Repaint Pile to Reflect Change } // setPileSelected() public boolean getPileSelected() // Accesses a Pile's 'selected' state { return this.pileSelected; } // getPileSelected() public void setPileHot(boolean passedSetting) // Sets a Pile's ID { this.pileHot = passedSetting; } // setPileSelected() public boolean getPileHot() // Accesses a Pile's ID { return this.pileHot; } // getPileSelected() public void setPileID(int passedID) // Sets a Pile's ID { this.pileID = passedID; } // setPileLength(1) public int getPileID() // Accesses a Pile's ID { return this.pileID; } // getPileTotal() public void setPileLength(int passedLength) // Sets a Pile's Length { this.pileLength = passedLength; } // setPileLength(1) public int getPileLength() // Accesses a Pile's Length { return this.pileLength; } // getPileLength() public int getPileTotal() // Accesses a Pile's Total Value { int chipTotal = 0; for (int i = 0; i < (this.getPileLength()); i++) // Sum all of the Pile's Chips chipTotal += this.pilePokerChip[i].getChipValue(); return chipTotal; } // getPileTotal() public void pushAChip(PokerChip chipToBePushed) // Puts a Chip into a pile { this.setPileLength(this.getPileLength() + 1); // Increment Pile length // Set new Chip's Values this.pilePokerChip[(this.getPileLength() - 1)].setChipColor(chipToBePushed.getChipColor()); this.pilePokerChip[(this.getPileLength() - 1)].setChipValue(chipToBePushed.getChipValue()); } // pileToPush(1) public void paintChipsVert(Graphics g) // Paints Chips Vertically { int chipLocationX = 10; // Sets initial draw points int chipLocationY = 10; if (this.getPileLength() == 0) // Empty Pile Handling { g.setColor(Color.black); g.drawString("Empty Pile",30,80); repaint(); } // if else // For each chip, check its color, then draw it. for (int i = 0; i < (this.getPileLength()); i++) { if ((i % 23 == 0) && (i != 0)) // Skip to next Column { chipLocationX += 35; chipLocationY = 10; } // if g.setColor(Color.black); // Give the Chip a Border g.fillOval((chipLocationX - 1),(chipLocationY - 1),(CHIPWIDTH + 2),(CHIPHEIGHT + 2)); if (this.pilePokerChip[i].getChipColor() == RED) // Check and Set Chip Color g.setColor(Color.red); if (this.pilePokerChip[i].getChipColor() == BLUE) g.setColor(Color.blue); if (this.pilePokerChip[i].getChipColor() == GREEN) g.setColor(Color.green); g.fillOval(chipLocationX,chipLocationY,CHIPWIDTH,CHIPHEIGHT); // Draw the Chip g.setColor(Color.black); // Draw the Chips Value on it. g.drawString(Integer.toString(this.pilePokerChip[i].getChipValue()),chipLocationX + 15, chipLocationY + 30); chipLocationY += 8; // Increment Draw Point location } // for loop } // paintChipsVert() public void paintChipsHoriz(Graphics g) // Paints Chips Horizontally { int chipLocationX = 10; // Set Initial Draw Points int chipLocationY = 10; g.setColor(Color.black); // For each chip, check its color, then draw it. for (int i = 0; i < (this.getPileLength()); i++) { if (((i % 19) == 0) && (i != 0)) // Skip to the Next Row { chipLocationX = 10; chipLocationY += 20; } // if g.setColor(Color.black); // Give the Chip a Border g.fillOval((chipLocationX - 1),(chipLocationY - 1),(CHIPWIDTH + 2),(CHIPHEIGHT + 2)); if (this.pilePokerChip[i].getChipColor() == RED) // Check and Set Chip Color g.setColor(Color.red); if (this.pilePokerChip[i].getChipColor() == BLUE) g.setColor(Color.blue); if (this.pilePokerChip[i].getChipColor() == GREEN) g.setColor(Color.green); g.fillOval(chipLocationX,chipLocationY,CHIPWIDTH,CHIPHEIGHT); // Draw the Chip g.setColor(Color.black); // Draw the Chips Value on it. g.drawString(Integer.toString(this.pilePokerChip[i].getChipValue()),chipLocationX + 15, chipLocationY + 30); chipLocationX += 10; // Increment Draw Point location } // for loop } // paintChipsVert() public void paintHotBorder(Graphics g) // Create a border indicating a 'hot' pile { this.setBackground(Color.black); g.setColor(Color.blue); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(Color.white); g.fillRect(5,5,this.getWidth() - 10,this.getHeight() - 10); } public void paintColdBorder(Graphics g) // Create a border indicating a 'cold' pile { this.setBackground(Color.black); g.setColor(Color.cyan); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(Color.white); g.fillRect(5,5,this.getWidth() - 10,this.getHeight() - 10); } public void paintSelectedBorder(Graphics g) // Create a border indicating a 'cold' pile { this.setBackground(Color.black); g.setColor(Color.red); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(Color.white); g.fillRect(5,5,this.getWidth() - 10,this.getHeight() - 10); } public void paintComponent( Graphics g ) // Pile Paints itself { super.paintComponent(g); if ((this.getPileHot() && !this.getPileSelected())) paintHotBorder(g); if ((!this.getPileHot() && !this.getPileSelected())) paintColdBorder(g); if (this.getPileSelected()) paintSelectedBorder(g); if (this.getPileLength() == 0) // Empty Pile Handling { g.setColor(Color.black); g.drawString("Empty Pile",30,80); } // if else if (this.getPileID() == MYPILE) // If the pile's one of mine, paint it vertically paintChipsVert(g); else paintChipsHoriz(g); // Otherwise, paint it horizontally } } // Pile.java