// This class illustrates some interactions that can be used to // develop board games. It uses the class Grid and its methods. import java.applet.*; import java.awt.*; import java.awt.event.*; public class BoardGame extends Applet implements MouseListener, ActionListener, ItemListener { // Variables used by the Applet int last_x = 0; int last_y = 0; Button clear_button; Choice user_choices; TextArea echo_area; TextField user_typing; Grid theBoard; String current_choice; char current_player; // Current player: ON, OFF, EX, or OH String current_message = ""; // Initializing the Frame public void init() { // Set the background color and listen for the mouse setBackground(Color.white); addMouseListener(this); // Create a button and add it to the Frame. clear_button = new Button("New Game"); clear_button.setForeground(Color.black); clear_button.setBackground(Color.lightGray); add(clear_button); clear_button.addActionListener(this); // Create a menu of user choices and add it to the // Frame. user_choices = new Choice(); user_choices.addItem("Redraw Board"); user_choices.addItem("Select X"); user_choices.addItem("Select O"); user_choices.addItem("Fill Square"); user_choices.addItem("Erase Square"); user_choices.addItem("Color Green"); user_choices.addItem("Color Black"); add(user_choices); user_choices.addItemListener(this); // Create a TextArea and a TextField and add them to // the Frame. echo_area = new TextArea(1, 40); echo_area.setEditable(false); add(echo_area); user_typing = new TextField(40); add(user_typing); user_typing.addActionListener(this); } public void mouseClicked(MouseEvent e) { Graphics g = this.getGraphics(); echo_area.setText("Mouse Clicked at " + e.getX() + ", " + e.getY()); last_x = e.getX(); last_y = e.getY(); if (theBoard.inGrid(last_x, last_y)) theBoard.set(theBoard.row, theBoard.col, current_player); } public void mousePressed (MouseEvent e) { } public void mouseReleased (MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } // Called when the user selects a Button or TextArea public void actionPerformed (ActionEvent e) { Graphics g = this.getGraphics(); // If the New Game button was clicked, clear the Frame if (e.getSource() == clear_button) { Rectangle r = this.bounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); echo_area.setText("New Game button selected "); g.setColor(Color.black); // and place a 5x5 board to the Frame, cellSize = 20 pixels // displaced horizontally 6 cells from the left margin theBoard = new Grid(g, 5, 20, 6); current_player = theBoard.OFF; current_choice = ""; } // Otherwise if some text was typed, handle that else if (e.getSource() == user_typing) { String s = user_typing.getText(); echo_area.setText("User Typing: " + s); } } // Called when the user makes a Choice selection public void itemStateChanged (ItemEvent e) { Graphics g = this.getGraphics(); current_choice = (String)(e.getItem()); echo_area.setText("Choice selected: " + current_choice); if (current_choice.equals("Redraw Board")) theBoard.showGrid(); if (current_choice.equals("Select X")) current_player = theBoard.EX; if (current_choice.equals("Select O")) current_player = theBoard.OH; if (current_choice.equals("Fill Square")) current_player = theBoard.ON; if (current_choice.equals("Erase Square")) current_player = theBoard.OFF; if (current_choice.equals("Color Green")) theBoard.cellColor(Color.green); if (current_choice.equals("Color Black")) theBoard.cellColor(Color.black); } }