From c5aa817339f73de0d2aa22b850ae238754575c61 Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Mon, 31 Mar 2025 10:02:42 +0200 Subject: [PATCH] Add SwingRenderer base code --- src/GomokuRenderer.java | 4 +- src/SwingRenderer.java | 103 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/src/GomokuRenderer.java b/src/GomokuRenderer.java index 1618ff1..09f9f0f 100644 --- a/src/GomokuRenderer.java +++ b/src/GomokuRenderer.java @@ -2,6 +2,6 @@ public abstract class GomokuRenderer { - public abstract void init(); - public abstract void update(GomokuGame game); + public abstract void init(GomokuGame game); + public abstract void update(); } diff --git a/src/SwingRenderer.java b/src/SwingRenderer.java index e8f8b8e..7e06c0f 100644 --- a/src/SwingRenderer.java +++ b/src/SwingRenderer.java @@ -1,12 +1,109 @@ +import javax.swing.SwingUtilities; +import javax.swing.BorderFactory; +import java.awt.Color; +import java.awt.Dimension; +import javax.swing.JFrame; +import javax.swing.JPanel; +import java.awt.Graphics; public class SwingRenderer extends GomokuRenderer { + private JFrame mainFrame; + private GomokuGame game; + private RendererPanel canvas; + public SwingRenderer() {} - @Override - public void init() {} + public static void main(String[] args) { + SwingRenderer s = new SwingRenderer(); + s.init(null); + } @Override - public void update(GomokuGame game) {} + public void init(GomokuGame game) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + mainFrame = new JFrame("Gomoku! - Projet L2"); + mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + mainFrame.setSize(960, 960); + canvas = new RendererPanel(); + mainFrame.add(canvas); + mainFrame.pack(); + mainFrame.setVisible(true); + } + }); + } + + @Override + public void update() { + // Update the game state and repaint the canvas + // This method should be called whenever the game state changes + canvas.draw(game); + } } + +class RendererPanel extends JPanel { + + public RendererBoard board; + + public RendererPanel() { + setBorder(BorderFactory.createLineBorder(Color.black)); + setBackground(Color.white); + board = new RendererBoard(8, 8); // Example board size + } + + public Dimension getPreferredSize() { + return new Dimension(800, 800); + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + + 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); + } + + public void draw(GomokuGame game) { + // Update the game state and repaint the canvas + board.update(game); + repaint(); + } +} + +class RendererBoard { + + private int width; + private int height; + private GomokuGame game; + + public RendererBoard(int width, int height) { + this.width = width; + this.height = height; + } + + public void update(GomokuGame game) { + // Update the board state based on the game + // This method should be called whenever the game state changes + this.game = game; + } + + public void drawBoard(Graphics g, int x, int y, int w, int h) { + // Draw the board here + g.setColor(Color.LIGHT_GRAY); + int cellSize = w / this.width; + + 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); + } + } + } +} \ No newline at end of file