This commit is contained in:
Cyprien111
2025-03-31 10:10:36 +02:00
2 changed files with 102 additions and 5 deletions

View File

@@ -2,6 +2,6 @@
public abstract class GomokuRenderer { public abstract class GomokuRenderer {
public abstract void init(); public abstract void init(GomokuGame game);
public abstract void update(GomokuGame game); public abstract void update();
} }

View File

@@ -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 { public class SwingRenderer extends GomokuRenderer {
private JFrame mainFrame;
private GomokuGame game;
private RendererPanel canvas;
public SwingRenderer() {} public SwingRenderer() {}
@Override public static void main(String[] args) {
public void init() {} SwingRenderer s = new SwingRenderer();
s.init(null);
}
@Override @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);
}
}
}
} }