// Game.java Copyright (c) 2004 brising.com import java.awt.BorderLayout; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.BorderFactory; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; /** I am the FindTheBlueBoxes game. I can run either as an application or as an applet. See http://www.brising.com/blackbox to play me, or invoke "java Game" from your command line.. */ public class Game extends JApplet implements ActionListener, PropertyChangeListener { // MAIN /////////////////////////////////////////////////////////////////////// static public void main ( String[] avs ) { JFrame oJFrame = new JFrame(); oJFrame.setDefaultCloseOperation(3); oJFrame.getContentPane().add(new Game()); oJFrame.setSize(200, 280); oJFrame.setVisible(true); } // STATIC VARIABLES /////////////////////////////////////////////////////////// static final int gkiOccupied = 5; // how many occupied boxes to I create? // PROTECTED STATIC METHODS /////////////////////////////////////////////////// /** Create a text field with the given titled border. */ static protected JTextField supplyTextField ( String asTitle ) { JTextField oJTextField = new JTextField(); oJTextField.setBorder(BorderFactory.createTitledBorder(asTitle)); oJTextField.setHorizontalAlignment(4); oJTextField.setEditable(false); return oJTextField; } // INSTANCE VARIABLES ///////////////////////////////////////////////////////// int hiGames; int hiRemaining; int hiScore; float hfScoreAverage; int hiScoreTotal; JButton hoButtonFinish; GamePanel hoGamePanel; JTextField hoTextFieldAverage; JTextField hoTextFieldGames; JTextField hoTextFieldScore; // CONSTRUCTORS /////////////////////////////////////////////////////////////// public Game ( ) { try { // make sure things look and work as expected UIManager.setLookAndFeel (UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception x) { x.printStackTrace(); } getContentPane().setLayout(new BorderLayout()); hoGamePanel = new GamePanel(10, 10); getContentPane().add(hoGamePanel, "Center"); JPanel oJPanel0 = new JPanel(); oJPanel0.setLayout(new BorderLayout()); JPanel oJPanel1 = new JPanel(); oJPanel1.setLayout(new GridLayout()); oJPanel1.add(hoTextFieldScore = supplyTextField("score")); oJPanel1.add(hoTextFieldGames = supplyTextField("games")); oJPanel1.add(hoTextFieldAverage = supplyTextField("average")); oJPanel0.add(oJPanel1, "North"); hoButtonFinish = new JButton("finish"); hoButtonFinish.addActionListener(this); oJPanel0.add(hoButtonFinish, "South"); getContentPane().add(oJPanel0, "South"); GameButton.addScoreListener(this); hiGames = 0; hiScore = 0; hfScoreAverage = 0.0F; hiScoreTotal = 0; reset(); } // PUBLIC INSTANCE METHODS //////////////////////////////////////////////////// /** If the action came from the finish button, then accumulate my score. */ public void actionPerformed ( ActionEvent ae ) { if (ae.getSource() == hoButtonFinish) finish(); } /** Accumulate my score statistics. */ public void finish ( ) { hiGames += 1; hiScoreTotal += hiScore; hfScoreAverage = 10 * hiScoreTotal / hiGames / 10.0F; reset(); } /** Respond to the objects I listen to. */ public void propertyChange ( PropertyChangeEvent ae ) { String s = ae.getPropertyName(); if (false){/* do nothing */} else if (GameButton.gksUpdateScore == s) setScore(hiScore - ((Integer)ae.getNewValue()).intValue()); else if (FieldButton.gksUpdateFound == s) hoButtonFinish.setEnabled(--hiRemaining == 0); } /** Prepare to play a new game. */ public void reset ( ) { hoTextFieldGames.setText(Integer.toString(hiGames)); hoTextFieldAverage.setText(Float.toString(hfScoreAverage)); hoButtonFinish.setEnabled(false); hoGamePanel.reset(hiRemaining = gkiOccupied); setScore(hoGamePanel.getScoreMax()); } /** Display the current score. */ public void setScore ( int ai ) { hoTextFieldScore.setText(Integer.toString(hiScore = ai)); } }