Magic.java

/*
        Class to create magic squares with an odd number of sides
        Written 10/24/99 by Kim Bruce
        Modified 04/09/02 by Jim Teresco
*/

import java.awt.*;
import java.applet.Applet;

public class Magic extends Applet {
    
    public static final int SIZE = 5;        // size of magic square
    
    private int[][] magicIntArray;        // array of integers held in magic square
    
    // Create and fill in two-dimensional array of buttons for magic square
    public void init() {
        
        // Give error message if size is even
        if (SIZE % 2 == 0) {                
            add(new Label("Magic squares must be of odd size"));
            add(new Label(SIZE + " is not odd!"));
        }
        
        // Create and layout buttons for magic square
        else {                                                
            // Set to be GridLayout so show in array
            setLayout(new GridLayout(SIZE,SIZE,2,2));        
            magicIntArray = new int[SIZE][SIZE];
            
            // fill in array with "magic" integers
            fillArray();                        

            // put magic integers in buttons
            for (int rowNum = 0; rowNum < SIZE; rowNum++) {        
                for (int colNum = 0; colNum < SIZE; colNum++) {
                    // add button with label from magicIntArray
                    Button aButton = new Button(""+magicIntArray[rowNum][colNum]);
                    aButton.addActionListener(new MagicListener(rowNum,colNum,this));
                    add(aButton);
                }
            }
            
            // Print sums of rows, columns and diags to show "magic"
            printSums();        
        }
    }
    
    
    private void fillArray() {
        
        int row = SIZE - 1;                // row and column for placing "1"
        int col = SIZE / 2;
        
        // find location for placing num
        for (int num = 1; num <= SIZE*SIZE; num++) {
            magicIntArray[row][col] = num;
            
            // go to right and down, wrapping if necessary
            int newRow = (row + 1) % SIZE;
            int newCol = (col + 1) % SIZE;
            
            // if empty add new number        
            if (magicIntArray[newRow][newCol] == 0) {
                row = newRow;
                col = newCol;
            }
            // if full write new number above old
            else        {                                                                
                row = (row - 1 + SIZE) % SIZE;
            }
        }
    }
    
    // Print sums of rows, columns, and diagonals
    private void printSums() {
        
        int sum,                // sum of elements in each row, col, or diagonal
            row, col;                 // row and column of element being added
        
        // print sum of each row
        for (row = 0; row < SIZE; row++) {
            sum = 0;
            for (col = 0; col < SIZE; col++) {
                sum = sum + magicIntArray[row][col];
            }
            System.out.println("The sum of elements in row "+row+" is "+ sum);
        }
        
        // print sum of each column
        for (col = 0; col < SIZE; col++)        {
            sum = 0;
            for (row = 0; row < SIZE; row++)        {
                sum = sum + magicIntArray[row][col];
            }
            System.out.println("The sum of elements in column "+col+" is "+ sum);
        }
        
        // print sum of major diagonal
        sum = 0;
        for (int diag = 0; diag < SIZE; diag++){
            sum = sum + magicIntArray[diag][diag];
        }
        System.out.println("The sum of elements in the main diagonal is "+ sum);
        
        // print sum of minor diagonal
        sum = 0;
        for (int diag = 0; diag < SIZE; diag++) {
            sum = sum + magicIntArray[diag][SIZE-diag-1];
        }
        System.out.println("The sum of elements in the reverse diagonal is "+ sum);
    }                
    
    public void printButtonInfo(int row, int col) {
        System.out.println("The button at ("+row+","+col+") has label "+magicIntArray[row][col]);
    }
}

MagicListener.java

/**
 * Class which listens to clicks on buttons in Magic square
 * Written 10/26/99 by Kim Bruce
 */
 
import java.awt.*;
import java.awt.event.*;

public class MagicListener implements ActionListener {
        
    // Row and column of button listened to
    private int row, col;                
    
    // The object containing the collection of buttons
    private Magic square;
    
    public MagicListener(int arow, int acol, Magic asquare) {
        row = arow;
        col = acol;
        square = asquare;
    }
    
    // when button is clicked ask square to print info on the button
    public void actionPerformed(ActionEvent evt) {
        square.printButtonInfo(row,col);
    }
}