SAE21_2023/SudokuDraw.java
2024-04-03 10:46:17 +02:00

44 lines
1.5 KiB
Java

import javax.swing.*;
import java.awt.*;
public class SudokuDraw extends JPanel {
private static final int GRID_SIZE = 3;
private static final int REGION_SIZE = 3;
private static final int CELL_SIZE = 50;
private int[][] grid;
public SudokuDraw(int[][] grid) {
this.grid = grid;
setPreferredSize(new Dimension(GRID_SIZE * REGION_SIZE * CELL_SIZE, GRID_SIZE * REGION_SIZE * CELL_SIZE));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Dessiner des lignes plus épaisses pour la grille 3x3
g.setColor(Color.BLACK);
for (int i = 0; i <= GRID_SIZE * REGION_SIZE; i++) {
if (i % REGION_SIZE == 0) {
g.fillRect(0, i * CELL_SIZE - 2, GRID_SIZE * REGION_SIZE * CELL_SIZE, 4);
g.fillRect(i * CELL_SIZE - 2, 0, 4, GRID_SIZE * REGION_SIZE * CELL_SIZE);
} else {
g.fillRect(0, i * CELL_SIZE - 1, GRID_SIZE * REGION_SIZE * CELL_SIZE, 2);
g.fillRect(i * CELL_SIZE - 1, 0, 2, GRID_SIZE * REGION_SIZE * CELL_SIZE);
}
}
// Dessiner les valeurs de la grille
g.setColor(Color.BLACK);
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
int value = grid[i][j];
if (value != 0) {
g.drawString(String.valueOf(value), j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE / 2);
}
}
}
}
}