SAE21_2023/SudokuUI.java

47 lines
1.7 KiB
Java

import javax.swing.*;
import java.awt.*;
public class SudokuUI extends JFrame {
private Grid grid;
private JButton[][] buttons;
public SudokuUI(Grid grid) {
this.grid = grid;
this.buttons = new JButton[9][9];
setTitle("Sudoku");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel gridPanel = new JPanel(new GridLayout(9, 9));
gridPanel.setBackground(Color.WHITE);
gridPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Boutons de la grille
createGridButtons(gridPanel);
int preferredSize = 50 * 9; // 50 est la taille approximative d'un bouton
gridPanel.setPreferredSize(new Dimension(preferredSize, preferredSize));
add(gridPanel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void createGridButtons(JPanel gridPanel) { // Création sudoku
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
JButton button = new JButton();
button.setFont(new Font("Arial", Font.BOLD, 24));
button.setContentAreaFilled(false);
button.setBorder(BorderFactory.createLineBorder(Color.BLACK));
button.addActionListener(new SudokuButtonListener(row, col, grid, buttons));
gridPanel.add(button);
buttons[row][col] = button;
// Mettez à jour le texte des boutons avec les valeurs de la grille Sudoku
int value = grid.getCell(row, col).getValue();
buttons[row][col].setText(value == 0 ? "" : String.valueOf(value));
}
}
}
}