Since I want everyone to have some experience coding a GUI, this is an individual assignment. You will not have a partner.
A magic square is an array of numbers from 1 to N squared, such that the sum of the numbers in each row, each column, and each of the two diagonals is the same. For example, here is a 3x3 magic square:
|
Rows: |
Columns: 8 + 3 + 4 = 15 1 + 5 + 9 = 15 6 + 7 + 2 = 15 |
Diagonals: 8 + 5 + 2 = 15 4 + 5 + 6 = 15 |
There is a simple algorithm for creating an odd-order magic square (a magic square with an odd number of rows and columns).
| Start by putting the number 1 in the center of the top row: |
|
|||||||||||||||||||||||||
|
Count upward and to the right. If you go off the top, drop down to the bottom. (That is, if the row number becomes negative, reset it to the array length minus one.) | |||||||||||||||||||||||||
Continue counting upward and to the right. If you go off the right, come
back in on the left. (Hint: use the modulo operator, %.) |
|
|||||||||||||||||||||||||
|
Continue counting upward and to the right. If you reach a square that is already occupied, go directly under the previous number. | |||||||||||||||||||||||||
| Continue in this manner as long as possible. |
|
|||||||||||||||||||||||||
|
When you go off the top right corner, the same rules apply; the next square is already occupied (by 11), so drop directly below the 15. Then continue. This technique works for any odd square. |
|
If you are having trouble understanding how the "wrapping around" works in the above algorithm, it may help to think of the array as being surrounded by identical ghost copies of itself. As you move up and to the right, if you go off the edge of the array, go ahead and put the next number into the surrounding array, and that will show you where the number belongs in the original, "real" array. For example, going diagonally upward from the initial This is only an aid for understanding; you should not use nine arrays in your program. |
|
Write a program that creates and displays magic squares. Here are the specific requirements:
|
|
The result should look something like this:
|
Use the following classes:
public class MagicSquareMaker extends JFrame public static void main(String args[])
method, which creates an instance of MagicSquareMaker and calls its makeGui method. The makeGui method creates the GUI (adding components and listeners) and displays it. MagicSquareMaker contains a method named fillPanel that takes
a JPanel and a magic square as arguments, removes the previous contents (if any) of the JPanel,
attaches a GridLayout manager to the JPanel, and creates and adds N*N numeric
labels to the JPanel, where N is the order of this magic square.public class MagicSquaregetMagicSquareArray to return the array representing the magic square.public class MagicSquareTestMagicSquare class.That is,
public class MagicSquareMaker extends JFrame {
public static void main(String args[]) {
new MagicSquareMaker().makeGui(5);
}
void makeGui(int n) {...}
void fillPanel(JPanel panel, MagicSquare square) {...}
}
public class MagicSquare {
public MagicSquare(int size) {...} // constructor
public int[][] getMagicSquareArray() {...}
}
public class MagicSquareTest {...}
Usually, when you create a GUI, you don't keep changing the layout managers or the components. If you do, you have to do a little extra work.
removeAll()Container, will remove all the JComponents
in that Container. Use this to remove the old JLabels
from your magic square, before changing the layout manager and putting in
the new JLabels.validate()Container, will tell Java that you
have changed the contents of that Container, and Java needs to
redraw it for you.Before 6am Friday April 5. Submit your zipped project file to Canvas. No other form of submission will be accepted.