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,9 +117,6 @@ public class Board {
} }
return pieces; return pieces;
} }
public boolean isSelected(int x, int y) { public boolean isSelected(int x, int y) {
@ -127,13 +124,6 @@ public class Board {
} }
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(
spriteSheet,
0,
0,
Math.round(5*cellWidth()),
Math.round(2*cellHeight()),
null
);
return;
}
if (myGame != null) {
// Draw Interface from state of simulator
float cellWidth = cellWidth();
float cellHeight = cellHeight();
g.setColor(Color.white); if(pieceSelectorMode) {
for(int x=0; x<myGame.getWidth();x++) { g.drawImage(
for (int y=0; y<myGame.getHeight(); y++) { spriteSheet,
boolean isSelect = myGame.isSelected(x,y); 0,
boolean isHighlight = myGame.isHighlighted(x,y); 0,
if(isSelect) { Math.round(5*cellWidth()),
g.setColor(Color.blue); Math.round(2*cellHeight()),
} null
if(isHighlight) { );
g.setColor(Color.yellow); return;
} }
if((x+y)%2==1 || isSelect || isHighlight) {
g.fillRect(
Math.round(x*cellWidth),
Math.round(y*cellHeight),
Math.round(cellWidth),
Math.round(cellHeight)
);
}
if(isHighlight || isSelect) {
g.setColor(Color.white);
}
} if (myGame != null) {
} // Define green theme colors
Color lightGreen = new Color(234, 235, 200); // Light green squares
Color darkGreen = new Color(119, 149, 86); // Dark green squares
Color highlightYellow = new Color(247, 247, 105); // Highlight color
Color selectBlue = new Color(66, 135, 245); // Selection color
g.setColor(Color.gray); // Draw chess board squares
for(int x=0; x<myGame.getWidth();x++) { float cellWidth = cellWidth();
int graphX = Math.round(x*cellWidth); float cellHeight = 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);
}
for (Piece piece : myGame.getPieces()) { for(int x=0; x<myGame.getWidth(); x++) {
drawPiece(g,piece); for (int y=0; y<myGame.getHeight(); y++) {
} boolean isSelect = myGame.isSelected(x,y);
} boolean isHighlight = myGame.isHighlighted(x,y);
// Set square color
if(isSelect) {
g.setColor(selectBlue);
}
else if(isHighlight) {
g.setColor(highlightYellow);
}
else {
g.setColor((x + y) % 2 == 0 ? lightGreen : darkGreen);
}
g.fillRect(
Math.round(x*cellWidth),
Math.round(y*cellHeight),
Math.round(cellWidth),
Math.round(cellHeight)
);
}
}
// Draw grid lines (optional)
g.setColor(new Color(0, 80, 0)); // Darker green for grid lines
for(int x=0; x<myGame.getWidth(); x++) {
int graphX = Math.round(x*cellWidth);
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();
}
});
panelTop.add(btnGo);
turnLabel = new JLabel("Turn : X"); JButton btnLoad = new JButton("Load File");
panelTop.add(turnLabel); btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicLoadFileButton();
}
});
panelRight.add(btnLoad);
JButton btnLoad = new JButton("Load File"); JButton btnSave = new JButton("Save To File");
btnLoad.addActionListener(new ActionListener() { btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clicLoadFileButton(); clicSaveToFileButton();
} }
}); });
panelRight.add(btnLoad); panelRight.add(btnSave);
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 btnAdder = new JButton("Add Piece"); JButton btnPieceSelector = new JButton("Piece Select");
btnAdder.addActionListener(new ActionListener() { btnPieceSelector.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clickButtonAdder(); clickButtonSelector();
} }
}); });
panelRight.add(btnAdder); panelRight.add(btnPieceSelector);
JButton btnPieceSelector = new JButton("Piece Select"); JButton btnUndo = new JButton("Undo");
btnPieceSelector.addActionListener(new ActionListener() { btnUndo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clickButtonSelector(); clicUndoButton();
} }
}); });
panelRight.add(btnPieceSelector); panelTop.add(btnUndo);
JButton btnUndo = new JButton("Undo"); chckbxWhiteAI = new JCheckBox("WhiteAI");
btnUndo.addActionListener(new ActionListener() { chckbxWhiteAI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
clicUndoButton(); clicAIToggle(true);
} }
});
panelTop.add(chckbxWhiteAI);
}); chckbxBlackAI = new JCheckBox("BlackAI");
panelTop.add(btnUndo); chckbxBlackAI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
clicAIToggle(false);
}
});
panelTop.add(chckbxBlackAI);
chckbxWhiteAI = new JCheckBox("WhiteAI"); // Create card layout for switching panels
chckbxWhiteAI.addActionListener(new ActionListener() { cardLayout = new CardLayout();
public void actionPerformed(ActionEvent arg0) { mainPanel = new JPanel(cardLayout);
clicAIToggle(true);
}
}); // Create panels
panelTop.add(chckbxWhiteAI); welcomePanel = new WelcomePanel(this);
chessBoard = new JPanelChessBoard(this);
chckbxBlackAI = new JCheckBox("BlackAI"); // Add panels to card layout
chckbxBlackAI.addActionListener(new ActionListener() { mainPanel.add(welcomePanel, "welcome");
public void actionPerformed(ActionEvent arg0) { mainPanel.add(chessBoard, "game");
clicAIToggle(false);
}
}); // Show welcome screen first
panelTop.add(chckbxBlackAI); cardLayout.show(mainPanel, "welcome");
panelDraw = new JPanelChessBoard(this); // Add main panel to frame
contentPane.add(panelDraw, BorderLayout.CENTER); contentPane.add(mainPanel, BorderLayout.CENTER);
} }
public void setStepBanner(String s) { public void showGameBoard() {
turnLabel.setText(s); cardLayout.show(mainPanel, "game");
} instantiateSimu(); // Initialize the game
}
public void setBorderBanner(String s) { public void setStepBanner(String s) {
borderLabel.setText(s); turnLabel.setText(s);
} }
public JPanelChessBoard getPanelDessin() { public void setBorderBanner(String s) {
return panelDraw; borderLabel.setText(s);
} }
public void instantiateSimu() { public JPanelChessBoard getPanelDessin() {
if(game==null) { return chessBoard;
game = new Game(this); }
panelDraw.setGame(game);
game.start();
}
}
public void clicButtonStart() { public void instantiateSimu() {
this.instantiateSimu(); if(game == null) {
game.setDefaultSetup(); game = new Game(this);
} chessBoard.setGame(game);
game.start();
}
}
public void clickButtonAdder() { public void clicButtonStart() {
panelDraw.toggleAdderMode(); this.instantiateSimu();
} game.setDefaultSetup();
public void clickButtonSelector() { }
panelDraw.togglePieceSelector();
}
private void clicUndoButton() { public void clickButtonAdder() {
if(game == null) { chessBoard.toggleAdderMode();
System.out.println("error : can't undo while no game present"); }
} else {
game.undoLastMove();
}
} public void clickButtonSelector() {
public void clicAIToggle(boolean isWhite) { chessBoard.togglePieceSelector();
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() { private void clicUndoButton() {
Game loadedSim = new Game(this); if(game == null) {
String fileName=SelectFile(); System.out.println("error : can't undo while no game present");
LinkedList<String> lines = new LinkedList<String>(); } else {
if (fileName.length()>0) { game.undoLastMove();
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() { public void clicAIToggle(boolean isWhite) {
String fileName=SelectFile(); if(game == null) {
if (fileName.length()>0) { System.out.println("error : can't activate AI while no game present");
String[] content = game.getFileRepresentation(); if(isWhite) {
writeFile(fileName, content); chckbxWhiteAI.setSelected(false);
} }else {
} chckbxBlackAI.setSelected(false);
}
} else {
game.toggleAI(isWhite);
}
}
public String SelectFile() { public void clicLoadFileButton() {
String s; Game loadedSim = new Game(this);
JFileChooser chooser = new JFileChooser(); String fileName=SelectFile();
chooser.setCurrentDirectory(new java.io.File(".")); LinkedList<String> lines = new LinkedList<String>();
chooser.setDialogTitle("Choose a file"); if (fileName.length()>0) {
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); try {
chooser.setAcceptAllFileFilterUsed(true); BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { String line = fileContent.readLine();
s=chooser.getSelectedFile().toString(); int colorID = 0;
} else { while (line != null) {
System.out.println("No Selection "); lines.add(line);
s=""; line = fileContent.readLine();
} }
return s; 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 writeFile(String fileName, String[] content) { public void clicSaveToFileButton() {
FileWriter csvWriter; String fileName=SelectFile();
try { if (fileName.length()>0) {
csvWriter = new FileWriter(fileName); String[] content = game.getFileRepresentation();
for (String row : content) { writeFile(fileName, content);
csvWriter.append(row); }
csvWriter.append("\n"); }
}
csvWriter.flush();
csvWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void update(int turnCount, boolean turnIsWhite) { public String SelectFile() {
turnLabel.setText("Turn : "+turnCount+", "+ (turnIsWhite?"White":"Black")); String s;
actionLabel.setText(panelDraw.isPieceAdderMode()?"Adding Piece": JFileChooser chooser = new JFileChooser();
(panelDraw.isPieceSelectorMode()?"Selecting Piece to Add": chooser.setCurrentDirectory(new java.io.File("."));
"Playing")); chooser.setDialogTitle("Choose a file");
this.repaint(); 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 eraseLabels() { public void writeFile(String fileName, String[] content) {
this.setStepBanner("Turn : X"); 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(chessBoard.isPieceAdderMode()?"Adding Piece":
(chessBoard.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);
}
}