/** * This Applet demonstrates the use of 2D arrays and some aspects of String * processing. * * A square of letters is provided, clicking on a square changes the letter * in the square either up or down the list of allowable characters. * * It could form a starting point for some entertaining games... */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class WordSquare extends java.applet.Applet implements ActionListener { static final int ROWS = 5; // number of rows in the word square static final int COLS = 5; // number of columns in the word square // The set of allowable characters in the alphabet static final String ALLOWABLE = "*abcdefghijklmnopqrstuvwxyz ,."; // When clicked, the square goes up one letter or down according to // the value in direction int direction = 1; Button square [][] = new Button[ROWS][COLS]; // button grid; Button increment = new Button ("inc"); // increment letters Button decrement = new Button ("dec"); // decrement letters /* * This method initialises the Applet and sets up the display */ public void init() { // a panel for the button grid Panel theSquare = new Panel(); theSquare.setLayout( new GridLayout(ROWS, COLS) ); // for each array element for (int c = 0; c < COLS; c++) { for (int r = 0; r < ROWS; r++) { int start = 0; // starting letter // take single letter from string of allowable characters square[r][c] = new Button(ALLOWABLE.substring(start, ++start) ); theSquare.add( square[r][c] ); // add the element square[r][c].addActionListener(this); // add action listener } } add(theSquare); // add the button panel add(increment); // add the inc and dec buttons add(decrement); // and listen for presses on them increment.addActionListener(this); decrement.addActionListener(this); increment.setEnabled(false); // disable the increment button at the start } /* * This method listens for the Button presses and processes them * * @param the Button which caused the method to be executed */ public void actionPerformed(ActionEvent e) { for (int c = 0; c < COLS; c++) { for (int r = 0; r < ROWS; r++) { // find out which element was clicked if (e.getSource() == square[r][c]) { // get the current letter value from the button label and // work out what the next one should be. int start = ALLOWABLE.indexOf( square[r][c].getLabel() ); start = (start + direction); // check to see if we went out of range if (start >= ALLOWABLE.length()) { start = 0; } if (start < 0) { start = ALLOWABLE.length()-1; } // set the label to the new letter square[r][c].setLabel(ALLOWABLE.substring(start, ++start)); } } } // check for a press on increment/decrement buttons if (e.getSource() == increment) // increment clicked { increment.setEnabled(false); // disable it decrement.setEnabled(true); // enable the decrement button direction = 1; // set direction up } if (e.getSource() == decrement) // decrement clicked { increment.setEnabled(true); // similar process decrement.setEnabled(false); direction = -1; // except direction down } } }