// Card.java // This file implements Set cards // Written by: Stuart Hansen // Date: January 9, 2002 import java.awt.*; import java.awt.image.*; import java.util.*; import javax.swing.*; import javax.swing.plaf.*; // The Card class represents cards on a Set Deck // Each card has four properties, a color, a shading pattern // a number of designs and a symbol. // In this class all objects are blue and ovals. public class Card extends JComponent { static final long serialVersionUID = 1L; static final int xSize = 150; // the width of the card static final int ySize = 200; // the height of the card static final Color backGround = new Color(240, 240, 240); // the background color for a card static Vector cardsInSet = new Vector(); private Color color; // the cards color (red, green or purple) private Shading shading; // the cards shading (filled, outlined or striped) private Location location; // the y location on the card where the shape is drawn private Symbol symbol; // the shape to draw (oval, diamond, or squiggle) private int index; // the index in the array of cards where this one is stored private int strokeIndex =0; // a toggle switch for bolding the edges of the card // the stroke width to use when drawing private Stroke cardStroke[] = {new BasicStroke(2.0f), new BasicStroke(5.0f)}; // An array of colors for the border of the card private Color strokeColor[] = {Color.black, Color.blue}; // the constructor simply sets each of the cards properties public Card (Color color, Shading shading, Location location, Symbol symbol) { this.color = color; this.shading = shading; this.location = location; this.symbol = symbol; setSize(xSize, ySize); setUI(new CardUI()); } // toString is included mostly for debugging public String toString () {return "Card " + color + " " + symbol;} private class CardUI extends ComponentUI { // paint draws the cards background and then places the shapes on it public void paint (Graphics g, JComponent c) { Graphics2D g2d=(Graphics2D) g; // the 2D graphics context Dimension d = getSize(); // the size of the card // This code draws the offwhite background g2d.setColor(backGround); g2d.fillRoundRect(1, 1, (int)d.getWidth()-2, (int)d.getHeight()-2, 20, 20); // Paint the border of the card g2d.setPaint(strokeColor[strokeIndex]); g2d.setStroke(cardStroke[strokeIndex]); g2d.drawRoundRect(1, 1, (int)d.getWidth()-2, (int)d.getHeight()-2, 20, 20); // We loop through this cards location drawing the symbol for (int i=0; i l; public Location () {l = new Vector();} public static Location getInstance() {return null;} public int size() {return l.size();} public Object get(int i) {return l.get(i);} protected boolean add(Integer o) {return l.add(o);} } class OneLoc extends Location { private static Location instance = new OneLoc(); public static Location getInstance() {return instance;} private OneLoc () { super(); add(new Integer(80)); } } class TwoLoc extends Location { private static Location instance = new TwoLoc(); public static Location getInstance() {return instance;} private TwoLoc () { super(); add(new Integer(55)); add(new Integer(105)); } } class ThreeLoc extends Location { private static Location instance = new ThreeLoc(); public static Location getInstance() {return instance;} private ThreeLoc () { super(); add(new Integer(30)); add(new Integer(80)); add(new Integer(130)); } } // A Symbol is one of the shapes that need drawing, oval, diamond or squiggle abstract class Symbol { public static Symbol getInstance() {return null;} abstract public void draw (Graphics g, int yValue, Color color, Color backGround, Shading shading); } // class Oval knows how to draw ovals class Oval extends Symbol { // As before, we use the singleton design pattern to enable comparisons private static Oval instance = new Oval(); private Oval() { } public static Symbol getInstance () {return instance;} // the draw method draws an oval public void draw (Graphics g, int yValue, Color color, Color backGround, Shading shading) { // Draw the oval Graphics2D g2d = (Graphics2D) g; g2d.setColor(color); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawOval(40, yValue, 70, 40); // Fill the oval g2d.setPaint(shading.getPaint(color, backGround)); g2d.fillOval(40, yValue, 70, 40); } } // class Diamond knows how to draw diamonds class Diamond extends Symbol { // The singleton design pattern to enable comparisons private static Diamond instance = new Diamond(); private Diamond() { } public static Symbol getInstance () {return instance;} // the draw method draws a diamond public void draw (Graphics g, int yValue, Color color, Color backGround, Shading shading) { // Draw the diamond Polygon p = new Polygon(); p.addPoint (75, yValue); p.addPoint (110, yValue+20); p.addPoint (75, yValue+40); p.addPoint (40, yValue+20); // Fill the diamond Graphics2D g2d = (Graphics2D) g; g2d.setColor(color); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawPolygon (p); g2d.setPaint(shading.getPaint(color, backGround)); g2d.fillPolygon(p); } } // class Squiggle knows how to draw a squiggle class Squiggle extends Symbol { // the singleton design pattern to enable comparisons private static Squiggle instance = new Squiggle(); private Squiggle() { } public static Symbol getInstance () {return instance;} // The draw method draws a squiggle public void draw (Graphics g, int yValue, Color color, Color backGround, Shading shading) { // Draw the squiggle Polygon p = new Polygon(); p.addPoint (40, yValue+30); p.addPoint (65, yValue); p.addPoint (85, yValue+20); p.addPoint (110, yValue+10); p.addPoint (85, yValue+40); p.addPoint (65, yValue+20); // Fill the squiggle Graphics2D g2d = (Graphics2D) g; g2d.setColor(color); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawPolygon (p); g2d.setPaint(shading.getPaint(color, backGround)); g2d.fillPolygon(p); } }