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"); button[row][col].addActionListener(baHandler); if(x[row] == col) button[row][col].setIcon(queenIcon); else button[row][col].setIcon(blankIcon); if(even(col+row)){ // checkerboards the chess board button[row][col].setBackground(Color.WHITE); }else{ button[row][col].setBackground(Color.BLACK); } smBox[row].add(button[row][col]); // adds button to row }//for col smBox[row].add(Box.createHorizontalGlue()); // centers contents left and right bigBox.add(smBox[row]); }//for row bigBox.add(Box.createVerticalGlue()); // glue on bottom of box to center top to bottom scrollPane.setViewportView(bigBox); } // setNewBoard() /* * Loads the file choose dialogue, then uses the file to remake the board */ private void loadFile(){ JFileChooser chooser = new JFileChooser(); int option = chooser.showOpenDialog(frame); if(option == JFileChooser.APPROVE_OPTION){ try{ BufferedReader in = new BufferedReader(new FileReader(chooser.getSelectedFile())); String line; StringTokenizer tok; LinkedList list = new LinkedList(); boolean moreNums = true; int num; do{ // collect all the numbers in a list line = in.readLine(); tok = new StringTokenizer(line); while(tok.hasMoreTokens() && moreNums){ num = Integer.parseInt(tok.nextToken()); if(num==-1){ moreNums = false; }else{ list.add(new Integer(num)); } } }while( (line!=null) && moreNums ); String newLabel=in.readLine(); while((line=in.readLine())!=null){ // get a String for the rest of the file newLabel = newLabel.concat(" ".concat(line)); } n=list.size(); int[] nums = new int[n]; int i=0; for(Iterator iter = list.iterator(); iter.hasNext(); ){ nums[i] = ((Integer)iter.next()).intValue(); i++; } setNewBoard(nums); setNorthLabel(); setSouthLabel(newLabel); SwingUtilities.updateComponentTreeUI(frame); }catch(IOException e){ System.out.println("Now that is very strange! There was and IOException!"); } } } // loadFile private void testLoadFile(int[] nums, String s){ for(int i=0, x=0; i=0) && (row=0) && (col