// SetGame.java // This program gives some sample code to illustrate how // to display cards to a JFrame // Written by: Stuart Hansen // Date: January 9, 2002 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SetGame extends JFrame { public static final long serialVersionUID = 1L; private final int BWIDTH = 660; // the initial board width private final int BHEIGHT = 750; // the board height private final int DELTA_WIDTH = 160; // the change in width when needed private JFrame frame; // a pointer to the application itself private Deck deck; // the deck of cards private Set set; // a data structure of up to three cards private JComponent c; // the container where the cards are displayed private Card cardsInContainer[] = new Card[21]; // the dealt cards private int numCardsInContainer=0; // the number of cards that have been dealt // pt is the array of points where the cards are displayed // We limit the size to 21, since 21 cards MUST have a set // Most of the time only the first 12 positions are used private Point pt[] = { new Point(10, 10), new Point(10, 220), new Point(10, 430), new Point(170, 10), new Point(170, 220), new Point(170, 430), new Point(330, 10), new Point(330, 220), new Point(330, 430), new Point(490, 10), new Point(490, 220), new Point(490, 430), new Point(650, 10), new Point(650, 220), new Point(650, 430), new Point(810, 10), new Point(810, 220), new Point(810, 430), new Point(970, 10), new Point(970, 220), new Point(970, 430), }; // The constructor sets up the Frame and adds some cards to it. public SetGame() { // Initialize the JFrame super( "Set Game" ); frame = this; setSize(BWIDTH, BHEIGHT); c = (JComponent) getContentPane(); c.setLayout(null); // add the menu bar to the application setJMenuBar(new SetMenuBar()); // set is the cards that have been clicked set = new Set(); // new up a deck and shuffle it deck = new Deck(); deck.shuffle(); // deal twelve cards to the display // each gets a mouse listener to handle clicks // We maintain a separate array (cardsInContainer) for // easy access to all the cards in the display for (int i=0; i<12; i++) { Card card = deck.dealOneCard(); card.setIndex(i); card.setLocation(pt[i]); card.addMouseListener (new CardMouseListener()); cardsInContainer[i] = card; c.add(card); } numCardsInContainer=12; // add the hint button to the display JButton hint = new JButton ("Hint"); hint.setBounds(200, 650, 80, 30); hint.addActionListener(new SetFinder()); c.add(hint); // add the exit button to the display JButton exit = new JButton("Exit"); exit.setBounds(40, 650, 80, 30); exit.addActionListener(new GameExit()); c.add(exit); // set the close operation and display the frame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); setVisible(true); } // This method searches the board for a set. It returns true and sets the // parameter to the set, if one if found. public boolean findSet (Set tester) { for (int i=0; i 12) { for (int k=0; k<3; k++) { Card card1 = (Card) set.get(k); int start = card1.getIndex(); c.remove(card1); // We pull the remaining cards back to the left for (int l=start+1; l New to start a new game."); } } // We have found a set, so we flash those cards else { tester.flashSet(); } } } // This class handles the exit button events private class GameExit implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } /** This class represents our menu */ private class SetMenuBar extends JMenuBar { public static final long serialVersionUID = 1L; // the menus and menu items private JMenu menu = new JMenu ("File"); private JMenuItem newItem = new JMenuItem("New"); private JMenuItem exitItem = new JMenuItem ("Exit"); private JMenu help = new JMenu ("Help"); private JMenuItem helpItem = new JMenuItem("Help"); // We use anonymous objects for some of our menu listeners public SetMenuBar (){ // newItem has an anonymous handler newItem.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent e) { // Remove the cards from the display Card card; for (int i=0; i