/** * Class: PokerChip * File: PokerChip.java * Original Author: Dave Peixotto * Creation Date: 2.23.2001 * Modified by: NULL * Last Modified: 2.23.2001 **************************************************************** * Class Specification: This class is used to represent a single poker * chip. **************************************************************** * Instance Variables: **color - private - this is the color of the chip **value - private - this is the value of the chip **displayValue - private - boolean - says if the value of the chip should be displayed **valueString - string representation of the value **X_SIZE - public - the X-axis dimension of the chip **Y_SIZE - public - theY-axis dimension of the chip **************************************************************** * Methods: **getValue() - gets the value of the chip **display() - displays the poker chip on the screen **************************************************************** */ import java.awt.*; // for display() method import java.awt.geom.*; public class PokerChip { //Class Variables public static final int X_SIZE = 50; public static final int Y_SIZE = 30; public static final int PAINT_GRADIENT = 5; // used for painting the chip public static final Color DISPLAY_VALUE_COLOR = Color.blue; //Instance Variables private Color color; private int value; private String valueString; private boolean displayValue = false; //Constructor public PokerChip(Color c, int val) { color = c; value = val; valueString = new Integer(val).toString(); } //accessor methods****************************************** /**getValue()**** * method simply returns the value of the chip. */ public int getValue(){ return value; } /**getColor**** * method returns the color of the chip */ public Color getColor() { return color; } /**getValueString()**** * method returns valuString */ public String getValueString() { return valueString; } /**getX_SIZE**** * method returns X_SIZE variable. Notice this value is also * available through constant X_SIZE */ public int getX_SIZE() { return X_SIZE; } /**getY_SIZE**** * method returns Y_SIZE variable. Notice this value is also * available through constant Y_SIZE */ public int getY_SIZE() { return Y_SIZE; } /**getDisplayValue**** * method returns the current value of displayValue */ public boolean getDisplayValue() { return displayValue; } //mutator methods******************************************* /**setDisplayValue()**** * method sets the variable displayValue */ public void setDisplayValue(boolean b){ if(b) displayValue = true; else displayValue = false; } //graphic methods******************************************* /**display()**** * Parameters: * Graphics2D g2--handle to the graphics context * int x--x position to display the chip at * int y--y position at which to display the chip **-**-**-**-** * This method displays the chip. It uses some Java 2 features to make the chip look * nice. */ public void display(Graphics2D g2, int x, int y) { //set pen color to black g2.setColor(Color.black); //make the pen thicker g2.setStroke(new BasicStroke(4.0f)); //sets painting attribute g2.setPaint(Color.black); //create a gradient fill to make the chip look cool GradientPaint colortowhite = new GradientPaint(x, y + Y_SIZE, color, x, y - PAINT_GRADIENT, Color.white); //draw black outline g2.draw(new Ellipse2D.Double(x, y, X_SIZE, Y_SIZE)); //change painting mode g2.setPaint(colortowhite); //fill in the chips color g2.fill(new Ellipse2D.Double(x, y, X_SIZE, Y_SIZE)); //draw the chips value if displayValue is true if(displayValue) { g2.setPaint(DISPLAY_VALUE_COLOR); //font metrics used to center text on the chip FontMetrics metrics = g2.getFontMetrics(); //draws the value in the middle of the chip g2.drawString( valueString, x + ((X_SIZE - metrics.stringWidth(valueString))/ 2), y+((Y_SIZE + (metrics.getHeight()/2) )/2) ); } } }