Actualiser src/windowInterface/MyInterface.java
This commit is contained in:
parent
e3b81bf579
commit
98e5ce6346
|
|
@ -36,48 +36,62 @@ import javax.swing.JToggleButton;
|
|||
import javax.swing.JRadioButton;
|
||||
import javax.swing.JCheckBox;
|
||||
|
||||
/**
|
||||
* This class creates the main window interface for a chess game.
|
||||
* It handles the game display, controls, and timer functionality.
|
||||
*/
|
||||
public class MyInterface extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = -6840815447618468846L;
|
||||
// Main panel that holds all components
|
||||
private JPanel contentPane;
|
||||
private JLabel turnLabel;
|
||||
private JLabel borderLabel;
|
||||
private JLabel speedLabel;
|
||||
// Labels to show game information
|
||||
private JLabel turnLabel; // Shows whose turn it is
|
||||
private JLabel borderLabel; // Shows border information
|
||||
private JLabel speedLabel; // Shows speed information
|
||||
// The chess board drawing panel
|
||||
private JPanelChessBoard panelDraw;
|
||||
// The game logic handler
|
||||
private Game game;
|
||||
// Label showing current action mode (playing, adding piece, etc.)
|
||||
private JLabel actionLabel;
|
||||
// Checkboxes to enable AI players
|
||||
private JCheckBox chckbxBlackAI;
|
||||
private JCheckBox chckbxWhiteAI;
|
||||
|
||||
// Timer-related components
|
||||
private JLabel whiteTimerLabel;
|
||||
private JLabel blackTimerLabel;
|
||||
private JCheckBox chckbxEnableTimer;
|
||||
private JSpinner timeSpinner;
|
||||
private JLabel whiteTimerLabel; // Shows white player's remaining time
|
||||
private JLabel blackTimerLabel; // Shows black player's remaining time
|
||||
private JCheckBox chckbxEnableTimer; // Checkbox to turn timer on/off
|
||||
private JSpinner timeSpinner; // Sets the initial time amount
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
* Create the chess game window with all its components.
|
||||
*/
|
||||
public MyInterface() {
|
||||
// Set up the main window properties
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(10, 10, 650, 650);
|
||||
setBounds(10, 10, 650, 650); // Set window position and size
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
contentPane.setLayout(new BorderLayout(0, 0));
|
||||
setContentPane(contentPane);
|
||||
|
||||
// Create top panel for game controls
|
||||
JPanel panelTop = new JPanel();
|
||||
contentPane.add(panelTop, BorderLayout.NORTH);
|
||||
|
||||
// Create right panel for additional controls
|
||||
JPanel panelRight = new JPanel();
|
||||
contentPane.add(panelRight, BorderLayout.EAST);
|
||||
panelRight.setLayout(new GridLayout(7, 1, 0, 5)); // Increased number of rows for timer controls
|
||||
panelRight.setLayout(new GridLayout(7, 1, 0, 5)); // 7 rows, 1 column layout
|
||||
|
||||
// Timer panel at the bottom
|
||||
// Create timer panel at the bottom
|
||||
JPanel timerPanel = new JPanel();
|
||||
timerPanel.setLayout(new GridLayout(2, 2, 5, 5));
|
||||
timerPanel.setLayout(new GridLayout(2, 2, 5, 5)); // 2 rows, 2 columns layout
|
||||
contentPane.add(timerPanel, BorderLayout.SOUTH);
|
||||
|
||||
// White timer
|
||||
// Set up white player's timer display
|
||||
JPanel whiteTimerPanel = new JPanel();
|
||||
whiteTimerLabel = new JLabel("10:00");
|
||||
whiteTimerLabel.setFont(new Font("Monospaced", Font.BOLD, 20));
|
||||
|
|
@ -86,7 +100,7 @@ public class MyInterface extends JFrame {
|
|||
whiteTimerPanel.add(whiteTimerLabel);
|
||||
timerPanel.add(whiteTimerPanel);
|
||||
|
||||
// Black timer
|
||||
// Set up black player's timer display
|
||||
JPanel blackTimerPanel = new JPanel();
|
||||
blackTimerLabel = new JLabel("10:00");
|
||||
blackTimerLabel.setFont(new Font("Monospaced", Font.BOLD, 20));
|
||||
|
|
@ -95,118 +109,140 @@ public class MyInterface extends JFrame {
|
|||
blackTimerPanel.add(blackTimerLabel);
|
||||
timerPanel.add(blackTimerPanel);
|
||||
|
||||
// Timer controls
|
||||
// Create timer control panel with enable checkbox and time spinner
|
||||
JPanel timerControls = new JPanel();
|
||||
timerControls.setLayout(new FlowLayout());
|
||||
|
||||
// Add checkbox to enable/disable timer
|
||||
chckbxEnableTimer = new JCheckBox("Enable Timer");
|
||||
chckbxEnableTimer.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
toggleTimer();
|
||||
toggleTimer(); // Call method to turn timer on/off
|
||||
}
|
||||
});
|
||||
timerControls.add(chckbxEnableTimer);
|
||||
|
||||
// Add spinner to set timer minutes
|
||||
timerControls.add(new JLabel("Minutes:"));
|
||||
timeSpinner = new JSpinner(new SpinnerNumberModel(10, 1, 60, 1));
|
||||
timeSpinner = new JSpinner(new SpinnerNumberModel(10, 1, 60, 1)); // Default 10 min, range 1-60
|
||||
timeSpinner.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
updateInitialTime();
|
||||
updateInitialTime(); // Call method when time value changes
|
||||
}
|
||||
});
|
||||
timerControls.add(timeSpinner);
|
||||
|
||||
timerPanel.add(timerControls);
|
||||
|
||||
// Create label to show current game action status
|
||||
actionLabel = new JLabel("Waiting For Start");
|
||||
panelTop.add(actionLabel);
|
||||
|
||||
// Create start/restart button
|
||||
JButton btnGo = new JButton("Start/Restart");
|
||||
btnGo.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicButtonStart();
|
||||
clicButtonStart(); // Call method when button is clicked
|
||||
}
|
||||
});
|
||||
panelTop.add(btnGo);
|
||||
|
||||
// Create turn indicator label
|
||||
turnLabel = new JLabel("Turn : X");
|
||||
panelTop.add(turnLabel);
|
||||
|
||||
// Create load game button
|
||||
JButton btnLoad = new JButton("Load File");
|
||||
btnLoad.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicLoadFileButton();
|
||||
clicLoadFileButton(); // Call method when button is clicked
|
||||
}
|
||||
});
|
||||
panelRight.add(btnLoad);
|
||||
|
||||
// Create save game button
|
||||
JButton btnSave = new JButton("Save To File");
|
||||
btnSave.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicSaveToFileButton();
|
||||
clicSaveToFileButton(); // Call method when button is clicked
|
||||
}
|
||||
});
|
||||
panelRight.add(btnSave);
|
||||
|
||||
// Create button to add pieces manually
|
||||
JButton btnAdder = new JButton("Add Piece");
|
||||
btnAdder.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clickButtonAdder();
|
||||
clickButtonAdder(); // Call method when button is clicked
|
||||
}
|
||||
});
|
||||
panelRight.add(btnAdder);
|
||||
|
||||
// Create button to select piece type when adding
|
||||
JButton btnPieceSelector = new JButton("Piece Select");
|
||||
btnPieceSelector.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clickButtonSelector();
|
||||
clickButtonSelector(); // Call method when button is clicked
|
||||
}
|
||||
});
|
||||
panelRight.add(btnPieceSelector);
|
||||
|
||||
// Create undo move button
|
||||
JButton btnUndo = new JButton("Undo");
|
||||
btnUndo.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicUndoButton();
|
||||
clicUndoButton(); // Call method when button is clicked
|
||||
}
|
||||
|
||||
});
|
||||
panelTop.add(btnUndo);
|
||||
|
||||
// Create checkbox to enable AI for white player
|
||||
chckbxWhiteAI = new JCheckBox("WhiteAI");
|
||||
chckbxWhiteAI.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicAIToggle(true);
|
||||
clicAIToggle(true); // Call method with true for white
|
||||
}
|
||||
|
||||
});
|
||||
panelTop.add(chckbxWhiteAI);
|
||||
|
||||
// Create checkbox to enable AI for black player
|
||||
chckbxBlackAI = new JCheckBox("BlackAI");
|
||||
chckbxBlackAI.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
clicAIToggle(false);
|
||||
clicAIToggle(false); // Call method with false for black
|
||||
}
|
||||
|
||||
});
|
||||
panelTop.add(chckbxBlackAI);
|
||||
|
||||
// Create and add the chess board panel
|
||||
panelDraw = new JPanelChessBoard(this);
|
||||
contentPane.add(panelDraw, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the turn display label
|
||||
*/
|
||||
public void setStepBanner(String s) {
|
||||
turnLabel.setText(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the border information label
|
||||
*/
|
||||
public void setBorderBanner(String s) {
|
||||
borderLabel.setText(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the chess board panel
|
||||
*/
|
||||
public JPanelChessBoard getPanelDessin() {
|
||||
return panelDraw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new game if none exists
|
||||
*/
|
||||
public void instantiateSimu() {
|
||||
if(game==null) {
|
||||
game = new Game(this);
|
||||
|
|
@ -215,23 +251,36 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start or restart the game with default piece setup
|
||||
*/
|
||||
public void clicButtonStart() {
|
||||
this.instantiateSimu();
|
||||
game.setDefaultSetup();
|
||||
|
||||
// Reset and start the timer if enabled
|
||||
// Start the timer if it's enabled
|
||||
if (game.isTimerEnabled()) {
|
||||
game.startTimer();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch to piece adding mode
|
||||
*/
|
||||
public void clickButtonAdder() {
|
||||
panelDraw.toggleAdderMode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch to piece selection mode
|
||||
*/
|
||||
public void clickButtonSelector() {
|
||||
panelDraw.togglePieceSelector();
|
||||
}
|
||||
|
||||
/**
|
||||
* Undo the last move
|
||||
*/
|
||||
private void clicUndoButton() {
|
||||
if(game == null) {
|
||||
System.out.println("error : can't undo while no game present");
|
||||
|
|
@ -240,6 +289,9 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn AI on or off for a player
|
||||
*/
|
||||
public void clicAIToggle(boolean isWhite) {
|
||||
if(game == null) {
|
||||
System.out.println("error : can't activate AI while no game present");
|
||||
|
|
@ -254,7 +306,7 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
|
||||
/**
|
||||
* Toggle timer on/off
|
||||
* Turn the chess timer on or off
|
||||
*/
|
||||
private void toggleTimer() {
|
||||
if (game == null) {
|
||||
|
|
@ -265,11 +317,11 @@ public class MyInterface extends JFrame {
|
|||
game.setTimerEnabled(isEnabled);
|
||||
|
||||
if (isEnabled) {
|
||||
game.startTimer();
|
||||
game.startTimer(); // Start the timer
|
||||
} else {
|
||||
game.stopTimer();
|
||||
game.stopTimer(); // Stop the timer
|
||||
|
||||
// Reset the displayed time
|
||||
// Reset the displayed time to initial value
|
||||
updateTimers(
|
||||
((Integer)timeSpinner.getValue()) * 60 * 1000,
|
||||
((Integer)timeSpinner.getValue()) * 60 * 1000
|
||||
|
|
@ -279,22 +331,22 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
|
||||
/**
|
||||
* Update initial time when spinner changes
|
||||
* Update the initial time when spinner value changes
|
||||
*/
|
||||
private void updateInitialTime() {
|
||||
if (game != null) {
|
||||
int minutes = (Integer)timeSpinner.getValue();
|
||||
game.setInitialTime(minutes);
|
||||
|
||||
// Update timer labels
|
||||
long timeMillis = minutes * 60 * 1000;
|
||||
// Update timer display labels
|
||||
long timeMillis = minutes * 60 * 1000; // Convert to milliseconds
|
||||
whiteTimerLabel.setText(ChessTimer.formatTime(timeMillis));
|
||||
blackTimerLabel.setText(ChessTimer.formatTime(timeMillis));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update timer display
|
||||
* Update both timer displays with current times
|
||||
*/
|
||||
public void updateTimers(long whiteTimeMillis, long blackTimeMillis) {
|
||||
whiteTimerLabel.setText(ChessTimer.formatTime(whiteTimeMillis));
|
||||
|
|
@ -302,7 +354,7 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
|
||||
/**
|
||||
* Handle time expiration
|
||||
* Show message when a player runs out of time
|
||||
*/
|
||||
public void timeExpired(boolean isWhiteExpired) {
|
||||
String message = (isWhiteExpired ? "White" : "Black") + " player's time has expired. " +
|
||||
|
|
@ -310,12 +362,16 @@ public class MyInterface extends JFrame {
|
|||
JOptionPane.showMessageDialog(this, message, "Game Over", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a game from a file
|
||||
*/
|
||||
public void clicLoadFileButton() {
|
||||
Game loadedSim = new Game(this);
|
||||
String fileName=SelectFile();
|
||||
String fileName = SelectFile(); // Open file chooser dialog
|
||||
LinkedList<String> lines = new LinkedList<String>();
|
||||
if (fileName.length()>0) {
|
||||
if (fileName.length() > 0) {
|
||||
try {
|
||||
// Read all lines from the file
|
||||
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
|
||||
String line = fileContent.readLine();
|
||||
int colorID = 0;
|
||||
|
|
@ -323,6 +379,7 @@ public class MyInterface extends JFrame {
|
|||
lines.add(line);
|
||||
line = fileContent.readLine();
|
||||
}
|
||||
// Set up the board with the file content
|
||||
loadedSim.setBoard(Arrays.stream(lines.toArray()).map(Object::toString).toArray(String[]::new));
|
||||
fileContent.close();
|
||||
} catch (Exception e) {
|
||||
|
|
@ -330,18 +387,24 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
game = loadedSim;
|
||||
panelDraw.setGame(game);
|
||||
this.repaint();
|
||||
this.repaint(); // Redraw the board
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the current game to a file
|
||||
*/
|
||||
public void clicSaveToFileButton() {
|
||||
String fileName=SelectFile();
|
||||
if (fileName.length()>0) {
|
||||
String fileName = SelectFile(); // Open file chooser dialog
|
||||
if (fileName.length() > 0) {
|
||||
String[] content = game.getFileRepresentation();
|
||||
writeFile(fileName, content);
|
||||
writeFile(fileName, content); // Write game data to file
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a file chooser dialog and return the selected file path
|
||||
*/
|
||||
public String SelectFile() {
|
||||
String s;
|
||||
JFileChooser chooser = new JFileChooser();
|
||||
|
|
@ -350,14 +413,17 @@ public class MyInterface extends JFrame {
|
|||
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
||||
chooser.setAcceptAllFileFilterUsed(true);
|
||||
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
s=chooser.getSelectedFile().toString();
|
||||
s = chooser.getSelectedFile().toString();
|
||||
} else {
|
||||
System.out.println("No Selection ");
|
||||
s="";
|
||||
s = "";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write string array to a file
|
||||
*/
|
||||
public void writeFile(String fileName, String[] content) {
|
||||
FileWriter csvWriter;
|
||||
try {
|
||||
|
|
@ -373,14 +439,24 @@ public class MyInterface extends JFrame {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the display with current game information
|
||||
*/
|
||||
public void update(int turnCount, boolean turnIsWhite) {
|
||||
turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black"));
|
||||
actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece":
|
||||
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add":
|
||||
// Update turn information
|
||||
turnLabel.setText("Turn : " + turnCount + ", " + (turnIsWhite ? "White" : "Black"));
|
||||
|
||||
// Update action mode information
|
||||
actionLabel.setText(panelDraw.isPieceAdderMode() ? "Adding Piece" :
|
||||
(panelDraw.isPieceSelectorMode() ? "Selecting Piece to Add" :
|
||||
"Playing"));
|
||||
this.repaint();
|
||||
|
||||
this.repaint(); // Redraw the window
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all information labels
|
||||
*/
|
||||
public void eraseLabels() {
|
||||
this.setStepBanner("Turn : X");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue