From 4c0d488480cabecf550568af28e6f1ab80781f38 Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Mon, 7 Apr 2025 09:01:35 +0200 Subject: [PATCH] Refactor RenderBoard class and add a proper visualisation with Swing --- src/RenderBoard.java | 89 +++++++++++++++++++++++++++++++++++------- src/SwingRenderer.java | 10 ++--- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/RenderBoard.java b/src/RenderBoard.java index a406c06..f184c3b 100644 --- a/src/RenderBoard.java +++ b/src/RenderBoard.java @@ -3,13 +3,32 @@ import java.awt.Graphics; class RenderBoard { - private int width; - private int height; - private int[][] boardState; // Example representation of the board state + private int boardWidth; + private int boardHeight; + public int renderWidth; + public int renderHeight; + private int cellSize; + private int borderThickness; + // private int[][] boardState; - public RenderBoard(int width, int height) { - this.width = width; - this.height = height; + public RenderBoard(int width, int height, int renderWidth, int renderHeight) { + this.boardWidth = width; + 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) { @@ -17,17 +36,59 @@ class RenderBoard { // This method should be called whenever the game state changes } - public void drawBoard(Graphics g, int x, int y, int w, int h) { - // Draw the board here + public void drawBoard(Graphics g, int x, int y) { + // Draw the global border 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++) { - for (int j = 0; j < this.height; j++) { - g.drawRect(x + i * cellSize, y + j * cellSize, cellSize, cellSize); - // Draw the tokens here - g.fillOval(x + i * cellSize, y + j * cellSize, cellSize, cellSize); + int i, j; + int cy = y + this.borderThickness; + for (i = 0; i < this.boardHeight; i++) { + int cx = x + this.borderThickness; + 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 + ); + } } \ No newline at end of file diff --git a/src/SwingRenderer.java b/src/SwingRenderer.java index 30b1670..444981d 100644 --- a/src/SwingRenderer.java +++ b/src/SwingRenderer.java @@ -50,7 +50,7 @@ class RenderCanvas extends JPanel { public RenderCanvas() { setBorder(BorderFactory.createLineBorder(Color.black)); setBackground(Color.white); - board = new RenderBoard(8, 8); // Example board size + board = new RenderBoard(15, 15, 600, 600); } public Dimension getPreferredSize() { @@ -62,11 +62,9 @@ class RenderCanvas extends JPanel { int midX = getWidth() / 2; int midY = getHeight() / 2; - int boardWidth = 400; // Example board width - int boardHeight = 400; // Example board height - int x = midX - (boardWidth / 2); - int y = midY - (boardHeight / 2); - board.drawBoard(g, x, y, boardWidth, boardHeight); + int x = midX - (this.board.renderWidth / 2); + int y = midY - (this.board.renderHeight / 2); + board.drawBoard(g, x, y); } public void draw(GomokuGame game) {