import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
/**
* This class is a program that demonstrates an N-Queens solution. It does so
* by displaying a gui on the screen showing a chess board of any size with queens
* placed in the spots designated by the solution input file.
* @author Darnell Arford
*/
public class NQueensDemo{
private int n; // number of queens
private JFrame frame;
private JDialog aboutWindow;
private JPanel mainPanel;
private JScrollPane scrollPane;
private JButton[][] button; // button[row][column]
private JLabel northLabel;
private JLabel southLabel;
private final String title = "N-Queens Demo";
private ButtonActionHandler baHandler;
private JMenuItem open;
private JMenuItem exit;
private JMenuItem clear;
private JMenuItem about;
ImageIcon queenIcon;
ImageIcon blankIcon;
int clickRow, clickCol;
/*
* Public constructor
*/
public NQueensDemo(){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){
System.err.println("Could not change look and feel");
}
n=0;
clickRow=clickCol=-1;
frame = new JFrame(title);
mainPanel = new JPanel();
scrollPane = new JScrollPane();
// Makes entire program end when the X is clicked
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
mainPanel.setLayout(new BorderLayout());
setNorthLabel();
firstSetSouthLabel();
queenIcon = new ImageIcon("queen_space.gif");
blankIcon = new ImageIcon("blank_space.gif");
baHandler = new ButtonActionHandler();
int[] initialBoard = {-1, -1, -1, -1, -1, -1, -1, -1};
setNewBoard(initialBoard);
setMenuBar();
mainPanel.add(scrollPane, BorderLayout.CENTER);
createAboutWindow();
frame.getContentPane().add(mainPanel);
frame.setSize(new Dimension(650, 600));
frame.show();
} // NQueensDemo()
private void createAboutWindow(){
aboutWindow = new JDialog(frame, "About N-Queens Demo");
JPanel aboutPanel = new JPanel();
String aboutInfo = ("" +
"
About N-Queens Demo
" +
"This program is a Demonstration of solutions to the N-Queens Problem
" +
"
Group Members:
" +
"Darnell Arford
Aleman-Meza
Ning Sho
Cheng Hu
WadeErtzberger" +
"");
JLabel aboutLabel = new JLabel(aboutInfo);
aboutPanel.add(aboutLabel);
aboutWindow.getContentPane().add(aboutPanel);
aboutWindow.pack();
}
/*
* Creates the North Label and adds it to the mainPanel
*/
private void setNorthLabel(){
if(n==0){
northLabel = new JLabel("" +
"N-Queens Demo: Press File\\Open to open a file" +
"
",
SwingConstants.CENTER);
}else{
mainPanel.remove(northLabel);
northLabel = new JLabel("" +
"N-Queens Demo for N=" + n +
"
",
SwingConstants.CENTER);
}
mainPanel.add(northLabel, BorderLayout.NORTH);
} // setNorthLabel()
private void firstSetSouthLabel(){
southLabel = new JLabel("Information about the solution will be displayed here",
SwingConstants.CENTER);
mainPanel.add(southLabel, BorderLayout.SOUTH);
}
/*
* Creates the South label and adds it to the mainPanel
*/
private void setSouthLabel(String str){
mainPanel.remove(southLabel);
southLabel = new JLabel(str, SwingConstants.CENTER);
mainPanel.add(southLabel, BorderLayout.SOUTH);
} // setSouthLabel()
/**
* creates the JMenuBar to be placed at the top of the Window
* puts the menuBar on the frame
*/
private void setMenuBar(){
MenuActionHandler maHandler = new MenuActionHandler();
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
JMenu options = new JMenu("Options");
JMenu help = new JMenu("Help");
open = new JMenuItem("Open");
exit = new JMenuItem("Exit");
clear = new JMenuItem("Clear Markings");
about = new JMenuItem("About");
open.addActionListener(maHandler);
about.addActionListener(maHandler);
clear.addActionListener(maHandler);
exit.addActionListener(maHandler);
file.setMnemonic('F');
open.setMnemonic('O');
exit.setMnemonic('X');
options.setMnemonic('P');
clear.setMnemonic('C');
help.setMnemonic('H');
about.setMnemonic('A');
menuBar.add(file);
menuBar.add(options);
menuBar.add(help);
file.add(open);
file.add(exit);
options.add(clear);
help.add(about);
frame.setJMenuBar(menuBar);
} // createMenuBar()
/*
* Creates a Box with boxes of buttons in it with queen icons in the columns and rows
* specified by x. x[row]=column where queens is located.
*/
private void setNewBoard(int[] x){
n = x.length;
Box bigBox = new Box(BoxLayout.Y_AXIS);
Box[] smBox = new Box[n];
bigBox.add(Box.createVerticalGlue()); // add glue on top of box to center contents top to bottom
button = new JButton[n][];
// for loop: creates horizontal boxes and adds them to the vertical box
for(int row= 0; rowSpace Number: " +col+ ", " +row+ "
" +
"Top left is 0, 0