SAE21_2023/SudokuDraw.java

47 lines
1.7 KiB
Java
Raw Normal View History

2024-04-03 10:46:17 +02:00
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) {
2024-04-03 11:51:47 +02:00
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Définir l'épaisseur de la ligne
g2d.setStroke(new BasicStroke(20)); // Épaisseur de ligne de 4 pixels
// Dessiner les lignes de la grille (en gras pour les contours)
for (int i = 0; i <= GRID_SIZE * REGION_SIZE; i++) {
if (i % REGION_SIZE == 0) {
g2d.drawRect(0, i * CELL_SIZE - 2, GRID_SIZE * REGION_SIZE * CELL_SIZE, 4);
g2d.drawRect(i * CELL_SIZE - 2, 0, 4, GRID_SIZE * REGION_SIZE * CELL_SIZE);
} else {
g2d.drawRect(0, i * CELL_SIZE - 1, GRID_SIZE * REGION_SIZE * CELL_SIZE, 2);
g2d.drawRect(i * CELL_SIZE - 1, 0, 2, GRID_SIZE * REGION_SIZE * CELL_SIZE);
}
}
2024-04-03 10:46:17 +02:00
// 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);
}
}
}
}
}