127 lines
3.8 KiB
Java
127 lines
3.8 KiB
Java
|
|
import javax.swing.BorderFactory;
|
|
import java.awt.Color;
|
|
import java.awt.Dimension;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JPanel;
|
|
import java.awt.Graphics;
|
|
import java.awt.event.MouseListener;
|
|
import java.awt.event.MouseEvent;
|
|
|
|
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) {
|
|
mainFrame = new JFrame("Gomoku! - Projet L2");
|
|
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
mainFrame.setSize(960, 960);
|
|
mainFrame.setResizable(false);
|
|
canvas = new RenderCanvas();
|
|
mainFrame.add(canvas);
|
|
mainFrame.pack();
|
|
mainFrame.setVisible(true);
|
|
mainFrame.addMouseListener(canvas);
|
|
|
|
// add a label component for the title
|
|
JLabel titleLabel = new JLabel("Gomoku Game!", JLabel.CENTER);
|
|
titleLabel.setVerticalAlignment(JLabel.CENTER);
|
|
titleLabel.setBackground(Color.WHITE);
|
|
titleLabel.setOpaque(true);
|
|
titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
|
|
// set the font size and color
|
|
titleLabel.setFont(titleLabel.getFont().deriveFont(24f));
|
|
|
|
mainFrame.add(titleLabel, "North");
|
|
mainFrame.pack();
|
|
|
|
// add a bottom label for the status
|
|
JLabel statusLabel = new JLabel("Status: Waiting for player...", JLabel.CENTER);
|
|
statusLabel.setVerticalAlignment(JLabel.CENTER);
|
|
statusLabel.setBackground(Color.WHITE);
|
|
statusLabel.setOpaque(true);
|
|
statusLabel.setBorder(BorderFactory.createEmptyBorder());
|
|
// set the font size and color
|
|
statusLabel.setFont(statusLabel.getFont().deriveFont(18f));
|
|
mainFrame.add(statusLabel, "South");
|
|
mainFrame.pack();
|
|
}
|
|
|
|
@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);
|
|
}
|
|
|
|
@Override
|
|
public void updateStatus(String status) {
|
|
// Update the status label
|
|
JLabel statusLabel = (JLabel) mainFrame.getContentPane().getComponent(1);
|
|
statusLabel.setText("Status: " + status);
|
|
}
|
|
}
|
|
|
|
class RenderCanvas extends JPanel implements MouseListener {
|
|
|
|
public RenderBoard board;
|
|
|
|
public RenderCanvas() {
|
|
setBorder(BorderFactory.createEmptyBorder());
|
|
setBackground(Color.white);
|
|
board = new RenderBoard(15, 15, 600, 600);
|
|
}
|
|
|
|
public Dimension getPreferredSize() {
|
|
return new Dimension(700, 700);
|
|
}
|
|
|
|
public void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
|
|
int midX = getWidth() / 2;
|
|
int midY = getHeight() / 2;
|
|
int x = midX - (this.board.renderWidth / 2);
|
|
int y = midY - (this.board.renderHeight / 2);
|
|
board.drawBoard(g, x, y);
|
|
}
|
|
|
|
public void draw(GomokuGame game) {
|
|
// Update the game state and repaint the canvas
|
|
board.update(game);
|
|
repaint();
|
|
}
|
|
|
|
@Override
|
|
public void mouseClicked(MouseEvent e) {
|
|
// Handle mouse click events
|
|
int x = e.getX();
|
|
int y = e.getY();
|
|
System.out.println("Mouse clicked at: " + x + ", " + y);
|
|
}
|
|
|
|
@Override
|
|
public void mousePressed(MouseEvent e) {
|
|
// Handle mouse press events
|
|
int x = e.getX();
|
|
int y = e.getY();
|
|
System.out.println("Mouse pressed at: " + x + ", " + y);
|
|
}
|
|
@Override
|
|
public void mouseReleased(MouseEvent e) {}
|
|
@Override
|
|
public void mouseEntered(MouseEvent e) {}
|
|
@Override
|
|
public void mouseExited(MouseEvent e) {}
|
|
} |