SAE21_2024/TextFilter.java

222 lines
6.7 KiB
Java
Raw Normal View History

2024-05-05 13:09:28 +02:00
/*
La classe <code>TextFilter</code> est utilisée pour gerer toutes les fonctions lié à la saisie des touches
@version 1.1
@author Thomas Follea, Yann Keraudren
*/
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;
import java.util.ArrayList;
2024-05-04 23:47:44 +02:00
import java.awt.Font;
public class TextFilter extends KeyAdapter {
private JTextField Text;
private int GRID_SIZE;
private JTextField[][] grid;
private int row;
private int col;
private ArrayList<Integer> bad_numbers = new ArrayList<Integer>();
2024-05-04 23:47:44 +02:00
private boolean errorDetected = true;
private int status;
2024-05-04 23:47:44 +02:00
public TextFilter (JTextField t,int GRID_SIZE, JTextField[][] grid, int i, int j, int status) {
this.Text = t;
this.GRID_SIZE = GRID_SIZE;
2024-05-04 23:47:44 +02:00
this.status = status;
this.grid = grid;
this.row = i;
this.col = j;
}
@Override
public void keyTyped(KeyEvent e) {
2024-04-29 15:08:14 +02:00
char chiffre = e.getKeyChar();
2024-04-29 15:08:14 +02:00
int taille = this.Text.getText().length();
2024-05-04 23:47:44 +02:00
// Si la longueur du texte est égale à 3 après l'ajout du chiffre, définir la taille de la police à 20
if (taille > 1) {
Text.setFont(new Font("Verdana", Font.BOLD, 20));
} else {
Text.setFont(new Font("Verdana", Font.BOLD, 40));
}
2024-05-05 13:09:28 +02:00
// Ignorer l'événement si le caractère entré n'est pas un chiffre entre 1 et 9 ou la touche de retour arrière
2024-05-04 23:47:44 +02:00
if ( ((chiffre < '1') || (chiffre > '9')) && (chiffre != KeyEvent.VK_BACK_SPACE)) {
2024-05-04 16:51:35 +02:00
e.consume(); // ignorer l'événement
}
2024-05-05 13:09:28 +02:00
// Vérifier le statut (1 == concepteur,2 == joueur)
2024-05-04 23:47:44 +02:00
if (status == 2){
if (errorDetected) {
if ((chiffre >= '1') && (chiffre <= '9')) {
GrilleValide(chiffre);
}
} else {
e.consume(); // empêcher la saisie de chiffres lorsque qu'une erreur est détectée
}
2024-05-05 13:09:28 +02:00
// permet de bloquer la saisie de doublon dans la même case
if (!Text.getText().isEmpty() && (chiffre != KeyEvent.VK_BACK_SPACE)) {
String existingText = Text.getText();
char[] existingChars = existingText.toCharArray();
for (char c : existingChars) {
if (chiffre == c) {
e.consume(); // empêcher la réécriture du même chiffre
return;
}
}
}
// Réinitialiser les couleurs si la touche de retour arrière est pressée lors d'une erreur
2024-05-04 23:47:44 +02:00
if ((!bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
Text.setBackground(Color.white);
while (!bad_numbers.isEmpty()) {
grid[bad_numbers.removeFirst()][bad_numbers.removeFirst()].setBackground(Color.white);
}
errorDetected = true; // Réinitialiser errorDetected après avoir corrigé les erreurs
}
2024-05-05 13:09:28 +02:00
// Défini la taille maiximum par case
2024-05-04 23:47:44 +02:00
if ( taille >= 4) {
e.consume();
}
} else if(status == 1){
2024-05-05 13:09:28 +02:00
// Traite la saisie pour savoir si le chiffer peut être posé
2024-05-04 23:47:44 +02:00
if ( taille > 1 && ((chiffre >= '1') && (chiffre <= '9')) && (chiffre != KeyEvent.VK_ENTER)) {
GrilleValide(chiffre);
}
2024-05-05 13:09:28 +02:00
// Réinitialiser les couleurs si la touche de retour arrière est pressée lors d'une erreur
2024-05-04 23:47:44 +02:00
if ((!bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
Text.setBackground(Color.white);
while(!bad_numbers.isEmpty()) {
grid[bad_numbers.removeFirst()][bad_numbers.removeFirst()].setBackground(Color.white);
}
}
2024-05-05 13:09:28 +02:00
// Taille maximum par case
2024-05-04 23:47:44 +02:00
if ( taille >= 1 ) {
e.consume();
}
2024-05-04 16:51:35 +02:00
}
2024-05-05 13:09:28 +02:00
// colorie la case si elle est bonne ou pas
2024-05-04 23:47:44 +02:00
if ( (bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
Coloriage();
}
}
2024-05-05 13:09:28 +02:00
// permet de validé les règles du sudoku pour un chiffre posé.
public void GrilleValide(char chiffre) {
int evaluant = Integer.parseInt(Character.toString(chiffre));
// Test de validité sur les lignes
for (int row2 = row + 1; row2 < this.GRID_SIZE; row2++) {
if (!grid[row2][col].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col].getText());
if (evaluant == comparateur) {
this.grid[row2][col].setBackground(Color.red);
this.grid[row][col].setBackground(Color.red);
this.bad_numbers.add(row2);
this.bad_numbers.add(col);
errorDetected = false;
}
}
}
2024-05-05 13:09:28 +02:00
for (int row2 = row - 1; row2 >= 0; row2--) {
if (!grid[row2][col].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col].getText());
if (evaluant == comparateur) {
this.grid[row2][col].setBackground(Color.red);
this.grid[row][col].setBackground(Color.red);
this.bad_numbers.add(row2);
this.bad_numbers.add(col);
errorDetected = false;
}
}
}
2024-05-05 13:09:28 +02:00
// Test de validité sur les colonnes
for (int col2 = col + 1; col2 < this.GRID_SIZE; col2++) {
if (!grid[row][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row][col2].getText());
if (evaluant == comparateur) {
2024-05-05 13:09:28 +02:00
this.grid[row][col2].setBackground(Color.red);
this.grid[row][col].setBackground(Color.red);
this.bad_numbers.add(row);
this.bad_numbers.add(col2);
2024-05-04 23:47:44 +02:00
errorDetected = false;
}
2024-05-05 13:09:28 +02:00
}
}
for (int col2 = col - 1; col2 >= 0; col2--) {
if (!grid[row][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row][col2].getText());
if (evaluant == comparateur) {
this.grid[row][col2].setBackground(Color.red);
this.grid[row][col].setBackground(Color.red);
this.bad_numbers.add(row);
this.bad_numbers.add(col2);
errorDetected = false;
}
}
}
// Vérifier la validité dans les régions
int rowregion = this.row/3*3;
int colregion = this.col/3*3;
for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
for (int col2 = colregion; col2 < colregion + 3; col2++) {
if (row2 != row && col2 != col ) {
if (!grid[row2][col2].getText().isEmpty()) {
int comparateur = Integer.parseInt(grid[row2][col2].getText());
if (evaluant == comparateur) {
this.grid[row2][col2].setBackground(Color.red);
this.grid[row][col].setBackground(Color.red);
this.bad_numbers.add(row2);
this.bad_numbers.add(col2);
errorDetected = false;
}
}
}
}
}
}
2024-05-04 16:51:35 +02:00
2024-05-05 13:09:28 +02:00
// permet de colorier en blanc si l'erreur n'est plus présente
2024-05-04 16:51:35 +02:00
public void Coloriage() {
2024-05-05 13:09:28 +02:00
//coloriage sur les lignes
for (int row2 = 0; row2 < this.GRID_SIZE; row2++) {
if (this.grid[row2][this.col].getBackground() == Color.red) {
this.grid[row2][this.col].setBackground(Color.white);
}
}
2024-05-04 16:51:35 +02:00
2024-05-05 13:09:28 +02:00
//coloriage sur les colonnes
for (int col2 = 0; col2 < this.GRID_SIZE; col2++) {
if (this.grid[this.row][col2].getBackground() == Color.red) {
this.grid[this.row][col2].setBackground(Color.white);
}
}
2024-05-04 16:51:35 +02:00
2024-05-05 13:09:28 +02:00
//coloriage des régions
int rowregion = this.row/3*3;
int colregion = this.col/3*3;
for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
for (int col2 = colregion; col2 < colregion + 3; col2++) {
if (this.grid[row2][col2].getBackground() == Color.red) {
this.grid[row2][col2].setBackground(Color.white);
}
}
2024-05-04 16:51:35 +02:00
}
}
}