// 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; Color current_color = Color.black; 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"); user_choices.addItem("Load grid from file"); add(user_choices); user_choices.addItemListener(this); // Create a TextArea and a TextField and add them to // the Frame. echo_area = new TextArea(2, 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) { // Clear the Frame Graphics g = this.getGraphics(); Rectangle r = this.bounds(); g.setColor(this.getBackground()); g.fillRect(r.x, r.y, r.width, r.height); g.setColor(current_color); if (e.getSource() == clear_button) { echo_area.setText("New Game button selected "); // Place a 5x5 board to the Frame, cellSize = 20 pixels // displaced horizontally 6 cells from the left margin theBoard = new Grid(g, 5, 15, 6); theBoard.cellColor(current_color); current_player = theBoard.OFF; current_choice = ""; } // Otherwise if some text was typed, handle that else if (e.getSource() == user_typing) { String s = (String)(user_typing.getText()); echo_area.setText("User Typing: " + s); if (current_choice.equals("Load grid from file")) { theBoard = new Grid(g, 12, 10, 6); theBoard.cellColor(current_color); theBoard.loadFromFile("file:///Macintosh%20HD/Desktop%20Folder/myApplet/" + 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")) current_color = Color.green; if (current_choice.equals("Color Black")) current_color = Color.black; theBoard.cellColor(current_color); if (current_choice.equals("Load grid from file")) echo_area.setText(echo_area.getText() + "\n" + "Now type the name of the file"); } }