SAE21_2024/TextFilter.java

224 lines
5.7 KiB
Java
Raw Normal View History

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));
}
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-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-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-04 23:47:44 +02:00
if ( taille >= 4) {
2024-05-04 23:47:44 +02:00
e.consume();
}
} else if(status == 1){
if ( taille > 1 && ((chiffre >= '1') && (chiffre <= '9')) && (chiffre != KeyEvent.VK_ENTER)) {
2024-05-04 23:47:44 +02:00
GrilleValide(chiffre);
}
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-04 23:47:44 +02:00
if ( taille >= 1 ) {
2024-05-04 23:47:44 +02:00
e.consume();
}
2024-05-04 16:51:35 +02:00
}
2024-05-04 23:47:44 +02:00
if ( (bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
2024-05-04 23:47:44 +02:00
Coloriage();
}
}
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);
2024-05-04 23:47:44 +02:00
errorDetected = false;
}
}
}
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);
2024-05-04 23:47:44 +02:00
errorDetected = false;
}
}
}
// 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) {
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;
}
}
}
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);
2024-05-04 23:47:44 +02:00
errorDetected = false;
}
}
}
// Vérifier la validité dans les régions
2024-05-04 16:51:35 +02:00
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);
2024-05-04 23:47:44 +02:00
errorDetected = false;
}
}
}
}
}
}
2024-05-04 16:51:35 +02:00
public void Coloriage() {
//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);
}
}
//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);
}
}
//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);
}
}
}
}
}