Color green + Welcome interface

This commit is contained in:
User 2025-05-21 21:23:38 +02:00
parent 2ba7cccbf4
commit 9fd3642203
4 changed files with 356 additions and 290 deletions

View File

@ -117,22 +117,12 @@ public class Board {
} }
return pieces; return pieces;
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
return hasSelectedPiece && selectedX == x && selectedY == y; return hasSelectedPiece && selectedX == x && selectedY == y;
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height) return; if (x < 0 || x >= width || y < 0 || y >= height) return;
@ -171,7 +161,7 @@ public class Board {
board[selectedX][selectedY] = null; board[selectedX][selectedY] = null;
selectedPiece.setX(x); selectedPiece.setX(x);
selectedPiece.setY(y); selectedPiece.setY(y);
selectedPiece.setMoved(true); // Marque la pièce comme ayant bougé selectedPiece.setMoved(true);
// Déplacement de la tour si roque // Déplacement de la tour si roque

View File

@ -77,66 +77,76 @@ public class JPanelChessBoard extends JPanel {
myGame = simu; myGame = simu;
} }
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
this.setBackground(Color.black); this.setBackground(new Color(0, 100, 0)); // Dark green background
if(pieceSelectorMode) {
g.drawImage( if(pieceSelectorMode) {
spriteSheet, g.drawImage(
0, spriteSheet,
0, 0,
Math.round(5*cellWidth()), 0,
Math.round(2*cellHeight()), Math.round(5*cellWidth()),
null Math.round(2*cellHeight()),
); null
return; );
} return;
if (myGame != null) { }
// Draw Interface from state of simulator
float cellWidth = cellWidth(); if (myGame != null) {
float cellHeight = cellHeight(); // Define green theme colors
Color lightGreen = new Color(234, 235, 200); // Light green squares
g.setColor(Color.white); Color darkGreen = new Color(119, 149, 86); // Dark green squares
for(int x=0; x<myGame.getWidth();x++) { Color highlightYellow = new Color(247, 247, 105); // Highlight color
for (int y=0; y<myGame.getHeight(); y++) { Color selectBlue = new Color(66, 135, 245); // Selection color
boolean isSelect = myGame.isSelected(x,y);
boolean isHighlight = myGame.isHighlighted(x,y); // Draw chess board squares
if(isSelect) { float cellWidth = cellWidth();
g.setColor(Color.blue); float cellHeight = cellHeight();
}
if(isHighlight) { for(int x=0; x<myGame.getWidth(); x++) {
g.setColor(Color.yellow); for (int y=0; y<myGame.getHeight(); y++) {
} boolean isSelect = myGame.isSelected(x,y);
if((x+y)%2==1 || isSelect || isHighlight) { boolean isHighlight = myGame.isHighlighted(x,y);
g.fillRect(
Math.round(x*cellWidth), // Set square color
Math.round(y*cellHeight), if(isSelect) {
Math.round(cellWidth), g.setColor(selectBlue);
Math.round(cellHeight) }
); else if(isHighlight) {
} g.setColor(highlightYellow);
if(isHighlight || isSelect) { }
g.setColor(Color.white); else {
} g.setColor((x + y) % 2 == 0 ? lightGreen : darkGreen);
}
}
} g.fillRect(
Math.round(x*cellWidth),
g.setColor(Color.gray); Math.round(y*cellHeight),
for(int x=0; x<myGame.getWidth();x++) { Math.round(cellWidth),
int graphX = Math.round(x*cellWidth); Math.round(cellHeight)
g.drawLine(graphX, 0, graphX, this.getHeight()); );
} }
for (int y=0; y<myGame.getHeight(); y++) { }
int graphY = Math.round(y*cellHeight);
g.drawLine(0, graphY, this.getWidth(), graphY); // Draw grid lines (optional)
} g.setColor(new Color(0, 80, 0)); // Darker green for grid lines
for(int x=0; x<myGame.getWidth(); x++) {
for (Piece piece : myGame.getPieces()) { int graphX = Math.round(x*cellWidth);
drawPiece(g,piece); g.drawLine(graphX, 0, graphX, this.getHeight());
} }
} for (int y=0; y<myGame.getHeight(); y++) {
int graphY = Math.round(y*cellHeight);
g.drawLine(0, graphY, this.getWidth(), graphY);
}
// Draw pieces
for (Piece piece : myGame.getPieces()) {
drawPiece(g, piece);
}
}
} }
private void drawPiece(Graphics g, Piece piece) { private void drawPiece(Graphics g, Piece piece) {

View File

@ -1,5 +1,6 @@
package windowInterface; package windowInterface;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.GridLayout; import java.awt.GridLayout;
@ -32,238 +33,257 @@ import javax.swing.JCheckBox;
public class MyInterface extends JFrame { public class MyInterface extends JFrame {
private static final long serialVersionUID = -6840815447618468846L; private static final long serialVersionUID = -6840815447618468846L;
private JPanel contentPane; private JPanel contentPane;
private JLabel turnLabel; private JLabel turnLabel;
private JLabel borderLabel; private JLabel borderLabel;
private JLabel speedLabel; private JLabel speedLabel;
private JPanelChessBoard panelDraw; private Game game;
private Game game; private JLabel actionLabel;
private JLabel actionLabel; private JCheckBox chckbxBlackAI;
private JCheckBox chckbxBlackAI; private JCheckBox chckbxWhiteAI;
private JCheckBox chckbxWhiteAI; private JPanelChessBoard chessBoard;
private WelcomePanel welcomePanel;
private CardLayout cardLayout;
private JPanel mainPanel;
/** /**
* Create the frame. * Create the frame.
*/ */
public MyInterface() { public MyInterface() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(10, 10, 650, 650); setBounds(10, 10, 650, 650);
contentPane = new JPanel(); contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0)); contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane); setContentPane(contentPane);
JPanel panelTop = new JPanel(); JPanel panelTop = new JPanel();
contentPane.add(panelTop, BorderLayout.NORTH); contentPane.add(panelTop, BorderLayout.NORTH);
JPanel panelRight = new JPanel(); JPanel panelRight = new JPanel();
contentPane.add(panelRight, BorderLayout.EAST); contentPane.add(panelRight, BorderLayout.EAST);
panelRight.setLayout(new GridLayout(4,1)); panelRight.setLayout(new GridLayout(4,1));
actionLabel = new JLabel("Waiting For Start");
panelTop.add(actionLabel);
actionLabel = new JLabel("Waiting For Start"); JButton btnGo = new JButton("Start/Restart");
panelTop.add(actionLabel); btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicButtonStart();
}
});
panelTop.add(btnGo);
JButton btnGo = new JButton("Start/Restart"); turnLabel = new JLabel("Turn : X");
btnGo.addActionListener(new ActionListener() { panelTop.add(turnLabel);
public void actionPerformed(ActionEvent arg0) {
clicButtonStart(); JButton btnLoad = new JButton("Load File");
} btnLoad.addActionListener(new ActionListener() {
}); public void actionPerformed(ActionEvent arg0) {
panelTop.add(btnGo); clicLoadFileButton();
}
});
panelRight.add(btnLoad);
turnLabel = new JLabel("Turn : X"); JButton btnSave = new JButton("Save To File");
panelTop.add(turnLabel); btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JButton btnLoad = new JButton("Load File"); clicSaveToFileButton();
btnLoad.addActionListener(new ActionListener() { }
public void actionPerformed(ActionEvent arg0) { });
clicLoadFileButton(); panelRight.add(btnSave);
}
});
panelRight.add(btnLoad);
JButton btnSave = new JButton("Save To File"); JButton btnAdder = new JButton("Add Piece");
btnSave.addActionListener(new ActionListener() { btnAdder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clicSaveToFileButton(); clickButtonAdder();
} }
}); });
panelRight.add(btnSave); panelRight.add(btnAdder);
JButton btnPieceSelector = new JButton("Piece Select");
btnPieceSelector.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clickButtonSelector();
}
});
panelRight.add(btnPieceSelector);
JButton btnAdder = new JButton("Add Piece"); JButton btnUndo = new JButton("Undo");
btnAdder.addActionListener(new ActionListener() { btnUndo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clickButtonAdder(); clicUndoButton();
} }
}); });
panelRight.add(btnAdder); panelTop.add(btnUndo);
JButton btnPieceSelector = new JButton("Piece Select"); chckbxWhiteAI = new JCheckBox("WhiteAI");
btnPieceSelector.addActionListener(new ActionListener() { chckbxWhiteAI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clickButtonSelector(); clicAIToggle(true);
} }
}); });
panelRight.add(btnPieceSelector); panelTop.add(chckbxWhiteAI);
chckbxBlackAI = new JCheckBox("BlackAI");
chckbxBlackAI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicAIToggle(false);
}
});
panelTop.add(chckbxBlackAI);
JButton btnUndo = new JButton("Undo"); // Create card layout for switching panels
btnUndo.addActionListener(new ActionListener() { cardLayout = new CardLayout();
public void actionPerformed(ActionEvent arg0) { mainPanel = new JPanel(cardLayout);
clicUndoButton();
} // Create panels
welcomePanel = new WelcomePanel(this);
chessBoard = new JPanelChessBoard(this);
// Add panels to card layout
mainPanel.add(welcomePanel, "welcome");
mainPanel.add(chessBoard, "game");
// Show welcome screen first
cardLayout.show(mainPanel, "welcome");
// Add main panel to frame
contentPane.add(mainPanel, BorderLayout.CENTER);
}
public void showGameBoard() {
cardLayout.show(mainPanel, "game");
instantiateSimu(); // Initialize the game
}
}); public void setStepBanner(String s) {
panelTop.add(btnUndo); turnLabel.setText(s);
}
chckbxWhiteAI = new JCheckBox("WhiteAI");
chckbxWhiteAI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicAIToggle(true);
}
}); public void setBorderBanner(String s) {
panelTop.add(chckbxWhiteAI); borderLabel.setText(s);
}
chckbxBlackAI = new JCheckBox("BlackAI");
chckbxBlackAI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicAIToggle(false);
}
}); public JPanelChessBoard getPanelDessin() {
panelTop.add(chckbxBlackAI); return chessBoard;
}
public void instantiateSimu() {
if(game == null) {
game = new Game(this);
chessBoard.setGame(game);
game.start();
}
}
panelDraw = new JPanelChessBoard(this); public void clicButtonStart() {
contentPane.add(panelDraw, BorderLayout.CENTER); this.instantiateSimu();
} game.setDefaultSetup();
}
public void clickButtonAdder() {
chessBoard.toggleAdderMode();
}
public void clickButtonSelector() {
chessBoard.togglePieceSelector();
}
public void setStepBanner(String s) { private void clicUndoButton() {
turnLabel.setText(s); if(game == null) {
} System.out.println("error : can't undo while no game present");
} else {
game.undoLastMove();
}
}
public void clicAIToggle(boolean isWhite) {
if(game == null) {
System.out.println("error : can't activate AI while no game present");
if(isWhite) {
chckbxWhiteAI.setSelected(false);
}else {
chckbxBlackAI.setSelected(false);
}
} else {
game.toggleAI(isWhite);
}
}
public void clicLoadFileButton() {
Game loadedSim = new Game(this);
String fileName=SelectFile();
LinkedList<String> lines = new LinkedList<String>();
if (fileName.length()>0) {
try {
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
String line = fileContent.readLine();
int colorID = 0;
while (line != null) {
lines.add(line);
line = fileContent.readLine();
}
loadedSim.setBoard(Arrays.stream(lines.toArray()).map(Object::toString).toArray(String[]::new));
fileContent.close();
} catch (Exception e) {
e.printStackTrace();
}
game = loadedSim;
chessBoard.setGame(game);
this.repaint();
}
}
public void setBorderBanner(String s) { public void clicSaveToFileButton() {
borderLabel.setText(s); String fileName=SelectFile();
} if (fileName.length()>0) {
String[] content = game.getFileRepresentation();
public JPanelChessBoard getPanelDessin() { writeFile(fileName, content);
return panelDraw; }
} }
public void instantiateSimu() { public String SelectFile() {
if(game==null) { String s;
game = new Game(this); JFileChooser chooser = new JFileChooser();
panelDraw.setGame(game); chooser.setCurrentDirectory(new java.io.File("."));
game.start(); chooser.setDialogTitle("Choose a file");
} chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
} chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
public void clicButtonStart() { s=chooser.getSelectedFile().toString();
this.instantiateSimu(); } else {
game.setDefaultSetup(); System.out.println("No Selection ");
} s="";
}
public void clickButtonAdder() { return s;
panelDraw.toggleAdderMode(); }
}
public void clickButtonSelector() { public void writeFile(String fileName, String[] content) {
panelDraw.togglePieceSelector(); FileWriter csvWriter;
} try {
csvWriter = new FileWriter(fileName);
private void clicUndoButton() { for (String row : content) {
if(game == null) { csvWriter.append(row);
System.out.println("error : can't undo while no game present"); csvWriter.append("\n");
} else { }
game.undoLastMove(); csvWriter.flush();
} csvWriter.close();
} catch (IOException e) {
} e.printStackTrace();
public void clicAIToggle(boolean isWhite) { }
if(game == null) { }
System.out.println("error : can't activate AI while no game present");
if(isWhite) { public void update(int turnCount, boolean turnIsWhite) {
chckbxWhiteAI.setSelected(false); turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black"));
}else { actionLabel.setText(chessBoard.isPieceAdderMode()?"Adding Piece":
chckbxBlackAI.setSelected(false); (chessBoard.isPieceSelectorMode()?"Selecting Piece to Add":
} "Playing"));
} else { this.repaint();
game.toggleAI(isWhite); }
}
} public void eraseLabels() {
this.setStepBanner("Turn : X");
public void clicLoadFileButton() { }
Game loadedSim = new Game(this); }
String fileName=SelectFile();
LinkedList<String> lines = new LinkedList<String>();
if (fileName.length()>0) {
try {
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
String line = fileContent.readLine();
int colorID = 0;
while (line != null) {
lines.add(line);
line = fileContent.readLine();
}
loadedSim.setBoard(Arrays.stream(lines.toArray()).map(Object::toString).toArray(String[]::new));
fileContent.close();
} catch (Exception e) {
e.printStackTrace();
}
game = loadedSim;
panelDraw.setGame(game);
this.repaint();
}
}
public void clicSaveToFileButton() {
String fileName=SelectFile();
if (fileName.length()>0) {
String[] content = game.getFileRepresentation();
writeFile(fileName, content);
}
}
public String SelectFile() {
String s;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Choose a file");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
s=chooser.getSelectedFile().toString();
} else {
System.out.println("No Selection ");
s="";
}
return s;
}
public void writeFile(String fileName, String[] content) {
FileWriter csvWriter;
try {
csvWriter = new FileWriter(fileName);
for (String row : content) {
csvWriter.append(row);
csvWriter.append("\n");
}
csvWriter.flush();
csvWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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":
"Playing"));
this.repaint();
}
public void eraseLabels() {
this.setStepBanner("Turn : X");
}
}

View File

@ -0,0 +1,46 @@
package windowInterface;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WelcomePanel extends JPanel {
private JButton startButton;
private MyInterface mainInterface;
public WelcomePanel(MyInterface mainInterface) {
this.mainInterface = mainInterface;
setLayout(new BorderLayout());
setBackground(new Color(0, 100, 0)); // Dark green background
// Create title label
JLabel titleLabel = new JLabel("Chess Game", SwingConstants.CENTER);
titleLabel.setFont(new Font("Serif", Font.BOLD, 48));
titleLabel.setForeground(Color.WHITE);
titleLabel.setBorder(BorderFactory.createEmptyBorder(50, 0, 30, 0));
// Create subtitle label
JLabel subtitleLabel = new JLabel("Let the game begin!", SwingConstants.CENTER);
subtitleLabel.setFont(new Font("Serif", Font.ITALIC, 24));
subtitleLabel.setForeground(new Color(234, 235, 200)); // Light green
// Create start button
startButton = new JButton("Start Game");
startButton.setFont(new Font("SansSerif", Font.BOLD, 18));
startButton.setBackground(new Color(119, 149, 86)); // Dark green
startButton.setForeground(Color.WHITE);
startButton.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 30));
startButton.addActionListener(e -> mainInterface.showGameBoard());
// Center panel for button
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(false);
buttonPanel.add(startButton);
// Add components
add(titleLabel, BorderLayout.NORTH);
add(subtitleLabel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
}
}