SAE21_2023/Enter.java

121 lines
4.7 KiB
Java
Raw Normal View History

2024-04-03 11:25:04 +02:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Enter 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;
private int[][] initialGrid; // Pour stocker la grille initiale
private int selectedRow = -1;
private int selectedCol = -1;
public Enter(int[][] grid) {
this.grid = grid;
// Copie de la grille initiale
this.initialGrid = new int[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
this.initialGrid[i][j] = grid[i][j];
}
}
setPreferredSize(new Dimension(GRID_SIZE * REGION_SIZE * CELL_SIZE, GRID_SIZE * REGION_SIZE * CELL_SIZE));
setFocusable(true); // Permet au JPanel de recevoir les événements de souris
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX() / CELL_SIZE;
int y = e.getY() / CELL_SIZE;
if (x >= 0 && x < GRID_SIZE * REGION_SIZE && y >= 0 && y < GRID_SIZE * REGION_SIZE) {
selectedRow = y;
selectedCol = x;
System.out.println("Case sélectionnée : (" + selectedRow + ", " + selectedCol + ")");
if (initialGrid[selectedRow][selectedCol] == 0) { // Ne permet la modification que pour les cellules vides
String valueStr = JOptionPane.showInputDialog(null, "Enter value for the selected cell:");
try {
int value = Integer.parseInt(valueStr);
2024-04-03 12:25:19 +02:00
if (isValidValue(value, selectedRow, selectedCol)) {
grid[selectedRow][selectedCol] = value;
repaint();
} else {
JOptionPane.showMessageDialog(null, "Not possible.");
}
2024-04-03 11:25:04 +02:00
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a number.");
}
} else {
JOptionPane.showMessageDialog(null, "You can't edit the default value.");
}
}
}
});
}
2024-04-03 12:08:54 +02:00
public int[][] getInitialGrid() {
2024-04-03 12:25:19 +02:00
return initialGrid;
2024-04-03 12:08:54 +02:00
}
2024-04-03 12:25:19 +02:00
private boolean isValidValue(int value, int row, int col) {
// Vérifier la ligne
for (int i = 0; i < grid[row].length; i++) {
if (grid[row][i] == value && i != col) {
return false;
}
}
// Vérifier la colonne
for (int i = 0; i < grid.length; i++) {
if (grid[i][col] == value && i != row) {
return false;
}
}
// Vérifier la région
int regionRow = row / REGION_SIZE * REGION_SIZE;
int regionCol = col / REGION_SIZE * REGION_SIZE;
for (int i = regionRow; i < regionRow + REGION_SIZE; i++) {
for (int j = regionCol; j < regionCol + REGION_SIZE; j++) {
if (grid[i][j] == value && (i != row || j != col)) {
return false;
}
}
}
return true;
}
2024-04-03 12:08:54 +02:00
2024-04-03 11:25:04 +02:00
@Override
2024-04-03 12:08:54 +02:00
protected void paintComponent(Graphics g) {
2024-04-03 12:25:19 +02:00
super.paintComponent(g);
2024-04-03 11:25:04 +02:00
2024-04-03 12:25:19 +02:00
// Dessiner les contours de la grille (en gras)
Graphics2D g2d = (Graphics2D) g;
Stroke oldStroke = g2d.getStroke(); // Sauvegarder le style de ligne précédent
g2d.setStroke(new BasicStroke(4)); // Épaisseur de ligne de 4 pixels
2024-04-03 11:51:47 +02:00
2024-04-03 12:25:19 +02:00
for (int i = 0; i <= GRID_SIZE * REGION_SIZE; i++) {
if (i % REGION_SIZE == 0) {
g2d.drawLine(0, i * CELL_SIZE, GRID_SIZE * REGION_SIZE * CELL_SIZE, i * CELL_SIZE);
g2d.drawLine(i * CELL_SIZE, 0, i * CELL_SIZE, GRID_SIZE * REGION_SIZE * CELL_SIZE);
}
2024-04-03 11:51:47 +02:00
}
2024-04-03 12:25:19 +02:00
// Rétablir le style de ligne précédent
g2d.setStroke(oldStroke);
2024-04-03 11:51:47 +02:00
2024-04-03 12:25:19 +02:00
// Dessiner les cases de la grille
for (int i = 0; i < GRID_SIZE * REGION_SIZE; i++) {
for (int j = 0; j < GRID_SIZE * REGION_SIZE; j++) {
g.setColor(Color.BLACK);
g.drawRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
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);
}
2024-04-03 11:25:04 +02:00
}
}
}
}