import java.util.*; import java.awt.*; import javax.swing.*; public class ChessBoard2 { // Instance variables. int size; int[] columns; // column[r] = which column in row r has a queen, if any // column[r] = -1, if this row has no queen // Constructor. public ChessBoard2 (int size) { this.size = size; columns = new int [size]; for (int i=0; i= 0) { return true; } // Now try the columns. for (int c=0; c= 0) { if (Math.abs(columns[r]-col) == Math.abs(row-r)) { return true; } } } return false; } public void display () { // Make the frame and set some of its parameters: JFrame f = new JFrame (); f.setSize (300,300); f.setTitle ("N Queens problem"); // Create our extension of the JPanel: ChessPanel drawPanel = new ChessPanel (); // We'll need to pass on the data: drawPanel.size = size; drawPanel.board = board; // Add this to the frame. Notice the strange syntax. f.getContentPane().add (drawPanel); // Bring up the frame. f.setVisible (true); } } class ChessPanel extends JPanel { int size; int[] columns; public void paintComponent (Graphics g) { // This is required. super.paintComponent (g); // First, compute the minimum dimension. The board size can't be larger. Dimension D = this.getSize(); int minD = D.height; if (D.width < minD) { minD = D.width; } // This is the size of each square. int cellSize = minD / size; // We'll draw an image for each queen. ImageTool im = new ImageTool (); Image queen = im.readImageFile ("queen.jpg"); // Now draw the chessboard. boolean isWhite = true; for (int i=0; i