
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 RenderCanvas canvas;

    public SwingRenderer() {}

    public static void main(String[] args) {
        SwingRenderer s = new SwingRenderer();
        s.init(null);
    }

    @Override
    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 RenderCanvas();
                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 RenderCanvas extends JPanel {

    public RenderBoard board;

    public RenderCanvas() {
        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.white);
        board = new RenderBoard(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();
    }
}