Refactor RenderBoard class and add a proper visualisation with Swing

This commit is contained in:
Dorian HAMDANI
2025-04-07 09:01:35 +02:00
parent 5db5fe280f
commit 4c0d488480
2 changed files with 79 additions and 20 deletions

View File

@@ -3,13 +3,32 @@ import java.awt.Graphics;
class RenderBoard { class RenderBoard {
private int width; private int boardWidth;
private int height; private int boardHeight;
private int[][] boardState; // Example representation of the board state public int renderWidth;
public int renderHeight;
private int cellSize;
private int borderThickness;
// private int[][] boardState;
public RenderBoard(int width, int height) { public RenderBoard(int width, int height, int renderWidth, int renderHeight) {
this.width = width; this.boardWidth = width;
this.height = height; this.boardHeight = height;
this.renderWidth = renderWidth;
this.renderHeight = renderHeight;
this.borderThickness = 5;
int cellWidth = renderWidth / width;
int cellHeight = renderHeight / height;
this.cellSize = Math.min(cellWidth, cellHeight);
}
public RenderBoard(int renderWidth, int renderHeight) {
this(
GomokuGame.DEFAULT_BOARD_WIDTH,
GomokuGame.DEFAULT_BOARD_HEIGHT,
renderWidth,
renderHeight
);
} }
public void update(GomokuGame game) { public void update(GomokuGame game) {
@@ -17,17 +36,59 @@ class RenderBoard {
// This method should be called whenever the game state changes // This method should be called whenever the game state changes
} }
public void drawBoard(Graphics g, int x, int y, int w, int h) { public void drawBoard(Graphics g, int x, int y) {
// Draw the board here // Draw the global border
g.setColor(Color.LIGHT_GRAY); g.setColor(Color.LIGHT_GRAY);
int cellSize = w / this.width; g.fillRect(
x,
y,
this.renderWidth + 2 * this.borderThickness,
this.renderHeight + 2 * this.borderThickness
);
g.setColor(Color.WHITE);
g.fillRect(
x + this.borderThickness,
y + this.borderThickness,
this.renderWidth,
this.renderHeight
);
for (int i = 0; i < this.width; i++) { int i, j;
for (int j = 0; j < this.height; j++) { int cy = y + this.borderThickness;
g.drawRect(x + i * cellSize, y + j * cellSize, cellSize, cellSize); for (i = 0; i < this.boardHeight; i++) {
// Draw the tokens here int cx = x + this.borderThickness;
g.fillOval(x + i * cellSize, y + j * cellSize, cellSize, cellSize); for (j = 0; j < this.boardWidth; j++) {
// Draw the border
g.setColor(Color.LIGHT_GRAY);
g.fillRect(
cx,
cy,
this.cellSize,
this.cellSize
);
g.setColor(Color.WHITE);
g.fillRect(
cx + this.borderThickness,
cy + this.borderThickness,
this.cellSize - 2 * this.borderThickness,
this.cellSize - 2 * this.borderThickness
);
// Draw the cell
this.drawToken(g, x, y, Color.BLUE);
cx += this.cellSize;
} }
cy += this.cellSize;
} }
} }
public void drawToken(Graphics g, int x, int y, Color color) {
// Draw the token at the specified position
g.setColor(color);
g.fillRect(
x + 2 * this.borderThickness,
y + 2 * this.borderThickness,
cellSize - 2 * borderThickness,
cellSize - 2 * borderThickness
);
}
} }

View File

@@ -50,7 +50,7 @@ class RenderCanvas extends JPanel {
public RenderCanvas() { public RenderCanvas() {
setBorder(BorderFactory.createLineBorder(Color.black)); setBorder(BorderFactory.createLineBorder(Color.black));
setBackground(Color.white); setBackground(Color.white);
board = new RenderBoard(8, 8); // Example board size board = new RenderBoard(15, 15, 600, 600);
} }
public Dimension getPreferredSize() { public Dimension getPreferredSize() {
@@ -62,11 +62,9 @@ class RenderCanvas extends JPanel {
int midX = getWidth() / 2; int midX = getWidth() / 2;
int midY = getHeight() / 2; int midY = getHeight() / 2;
int boardWidth = 400; // Example board width int x = midX - (this.board.renderWidth / 2);
int boardHeight = 400; // Example board height int y = midY - (this.board.renderHeight / 2);
int x = midX - (boardWidth / 2); board.drawBoard(g, x, y);
int y = midY - (boardHeight / 2);
board.drawBoard(g, x, y, boardWidth, boardHeight);
} }
public void draw(GomokuGame game) { public void draw(GomokuGame game) {