Skip to content

Commit dbe2759

Browse files
authored
Add files via upload
1 parent 2aee555 commit dbe2759

File tree

4 files changed

+390
-0
lines changed

4 files changed

+390
-0
lines changed

ChessBoard.java

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
2+
3+
import java.awt.Color;
4+
import java.awt.Dimension;
5+
import java.awt.GridLayout;
6+
import java.awt.Image;
7+
import java.awt.Toolkit;
8+
import java.awt.BorderLayout;
9+
import java.awt.Container;
10+
import java.awt.event.*;
11+
import java.io.File;
12+
import java.io.IOException;
13+
import javax.imageio.ImageIO;
14+
import javax.swing.JToggleButton;
15+
import javax.swing.JButton;
16+
import javax.swing.JLabel;
17+
import javax.swing.JPanel;
18+
import javax.swing.JFrame;
19+
import javax.swing.JToolBar;
20+
import javax.swing.ImageIcon;
21+
/*****************************************************************************
22+
* This is the UI class. It handles all (or as much as possible) of the
23+
* front-end implementation. It inherits from the JPanel class.
24+
*****************************************************************************/
25+
@SuppressWarnings("serial")
26+
public class ChessBoard extends JPanel{
27+
private static JLabel message = new JLabel("Ready to play!");
28+
private static JButton nextButt = new JButton("Next Solution");
29+
private static JToggleButton clickedButt;
30+
private static int totalSolutions = 0;
31+
public static int solCount = 1;
32+
public static JToggleButton[][] tilesButt = new JToggleButton[8][8];
33+
/**********************************************************************
34+
* creates the toolbar for the UI.
35+
**********************************************************************/
36+
public static JToolBar toolbar(JFrame frame){
37+
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
38+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39+
40+
JToolBar toolbar = new JToolBar(); // toolbar constructor
41+
toolbar.setRollover(true); // turn on the rollover visual effect
42+
toolbar.addSeparator(); // separate toolbar content
43+
JButton newGameButt = new JButton("New Game"); // construct 'New Game' JButton
44+
toolbar.add(newGameButt); // add button to toolbar
45+
toolbar.addSeparator(); // separate toolbar content
46+
toolbar.add(nextButt); // add button to toolbar (defined as an instance variable)
47+
nextButt.setEnabled(false); // disable nextButt until user clicks board
48+
toolbar.addSeparator(); // separate toolbar content
49+
toolbar.add(message); // add JLabel message to toolbar
50+
51+
Container contentPane = frame.getContentPane();
52+
contentPane.add(toolbar, BorderLayout.NORTH);
53+
frame.setSize(screenSize.width, 150);
54+
toolbar.setFloatable(false);
55+
56+
newGameButt.addActionListener( new ActionListener() {// add action listener
57+
@Override // override other buttons(/methods with same name)
58+
public void actionPerformed(ActionEvent e) {
59+
clearBoard(); // revert board to original
60+
Queen.resetData(); // reset stack and list to null
61+
nextButt.setEnabled(false); // re-disable next solution button
62+
clickedButt.setEnabled(true); // re-enable clicked button
63+
solCount = 1; // reset solCount
64+
setMSG("Ready to Play!"); // set UI message
65+
}
66+
});
67+
68+
nextButt.addActionListener( new ActionListener() { // add action listener
69+
@Override // override other buttons(/methods with same name)
70+
public void actionPerformed(ActionEvent e) {
71+
Queen.iterate(); // get and print next solution in Queen.
72+
solCount++; // increment solCount
73+
setMSG("Solution " + solCount + "/" + totalSolutions); // set UI message
74+
}
75+
});
76+
return toolbar; // return the toolbar to main to be placed into frame
77+
}
78+
79+
/*************************************************
80+
* Setter method: For 'totalSolutions' variable.
81+
*************************************************/
82+
public static void setTotSol(int x){
83+
totalSolutions = x; // set totalSolutions as provided int
84+
}
85+
86+
/************************************************************
87+
* Setter method: Alters message at top of UI.
88+
************************************************************/
89+
public static void setMSG(String x){
90+
message.setText(x); // set message as provided string
91+
}
92+
93+
/************************************************************
94+
* Method called when every solution has been displayed.
95+
************************************************************/
96+
public static void printEnd(){
97+
setMSG("Solution " + (solCount) + "/" + totalSolutions); // print current/total solutions
98+
nextButt.setEnabled(false); // disable nextButt so currently displayed solution isn't cleared
99+
}
100+
101+
/**************************************************************
102+
* Method that clears the board of queen images and selections.
103+
* Does not enable the clicked button to retain its colour.
104+
**************************************************************/
105+
public static void clearBoard(){
106+
for (int x = 0; x < 8; x++) { // loop over board,
107+
for (int y = 0; y < 8; y++) {
108+
JToggleButton temp = tilesButt[x][y]; // each button..
109+
temp.setIcon(null); // remove icon
110+
temp.setSelected(false); // removes selections
111+
if(temp != clickedButt){ // unless the clicked button; enable
112+
temp.setEnabled(true);
113+
}
114+
}
115+
}
116+
}
117+
118+
/********************************************************************************
119+
* ChessBoard object. This is the checkered grid of the Chessboard, created from
120+
* 8x8 grid of JToggleButtons that the user can interact with.
121+
********************************************************************************/
122+
public ChessBoard() { // constructor
123+
Dimension dims = new Dimension(64, 64); // instantize dimension for tiles
124+
setLayout(new GridLayout(8, 8)); // set the board layout
125+
for (int x = 0; x < 8; x++) { // loop through the 8 x 8 grid..
126+
for (int y = 0; y < 8; y++) {
127+
JToggleButton b = new JToggleButton(); // button to add to each tile
128+
b.setPreferredSize(dims); // set dimension for tiles
129+
b.setMinimumSize(dims); // set minimum dimension for tiles
130+
if ((x + y + 1) % 2 == 0) { // set the checkered colours for the board
131+
b.setBackground(Color.WHITE);
132+
} else {
133+
b.setBackground(Color.BLACK);
134+
}
135+
add(b); // add button
136+
tilesButt[x][y] = b; // set location
137+
final int i = x; // column number (to send to Queen)
138+
139+
b.addActionListener( new ActionListener() { // adds listener to each button
140+
@Override
141+
public void actionPerformed(ActionEvent e) {// when clicked..
142+
clickedButt = b; // set clicked button variable
143+
Queen.iterate(b, i); // calls the iterate method from the Queen class to begin the algorithm
144+
queenIMG(b); // add queen image to board
145+
b.setEnabled(false); // essentially highlighting the image through the iterations
146+
nextButt.setEnabled(true); // enables the 'next solution' button
147+
setMSG("Solution " + solCount + "/" + totalSolutions); // set UI message
148+
}
149+
});
150+
}
151+
}
152+
}
153+
154+
/******************************************************************************
155+
* method to import and set the queen image. First gets the image from location,
156+
* scales it down to fit inside the tile (64 x 64) and assigns to an
157+
* ImageIcon. It then sets this image at inside the required button.
158+
******************************************************************************/
159+
public static void queenIMG(JToggleButton q){ // method to import image
160+
try {
161+
final Image queenBuff = ImageIO.read(new File("queenIMG.png"));
162+
Image queenImage = queenBuff.getScaledInstance(64, 64, java.awt.Image.SCALE_SMOOTH);
163+
final ImageIcon queenIcon = new ImageIcon(queenImage);
164+
q.setIcon(queenIcon);
165+
}catch (IOException e){
166+
System.out.println(e.toString());
167+
System.out.println("Could not find file 'queenIMG.png'");
168+
}
169+
}
170+
}

Main.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.awt.BorderLayout;
2+
import javax.swing.JFrame;
3+
import javax.swing.JToolBar;
4+
/*******************************************************
5+
* the main class of the program; where it is run from.
6+
*******************************************************/
7+
public class Main{
8+
public static void main(String[] args) {
9+
setUI(); // calls method to setup the user interface
10+
}
11+
public static void setUI(){ // method to set UI
12+
JFrame f = new JFrame("Chess"); // new frame to house the UI
13+
JToolBar tools = ChessBoard.toolbar(f); // create toolbar from 'chessboard' class
14+
ChessBoard chessBoard = new ChessBoard(); // create chessboard object
15+
16+
f.add(tools,BorderLayout.PAGE_START); // add toolbar to frame
17+
f.add(chessBoard); // add chessboard to frame
18+
f.pack(); // pack frame components
19+
f.setLocationRelativeTo(null); // ensure position
20+
f.setVisible(true); // set frame visible
21+
}
22+
}

0 commit comments

Comments
 (0)