2024-04-28 13:16:31 +02:00
|
|
|
import java.awt.event.*;
|
|
|
|
import javax.swing.*;
|
2024-05-03 19:00:41 +02:00
|
|
|
import java.awt.Color;
|
2024-05-04 15:14:03 +02:00
|
|
|
import java.util.ArrayList;
|
2024-04-28 13:16:31 +02:00
|
|
|
|
|
|
|
|
2024-05-03 19:00:41 +02:00
|
|
|
public class TextFilter extends KeyAdapter {
|
2024-04-28 13:16:31 +02:00
|
|
|
|
|
|
|
private JTextField Text;
|
|
|
|
|
2024-05-03 19:00:41 +02:00
|
|
|
private int GRID_SIZE;
|
|
|
|
|
|
|
|
private JTextField[][] grid;
|
|
|
|
|
|
|
|
private int row;
|
|
|
|
|
|
|
|
private int col;
|
|
|
|
|
2024-05-04 15:14:03 +02:00
|
|
|
private ArrayList<Integer> bad_numbers = new ArrayList<Integer>();
|
2024-05-03 19:00:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
public TextFilter (JTextField t,int GRID_SIZE, JTextField[][] grid, int i, int j) {
|
2024-04-28 13:16:31 +02:00
|
|
|
|
|
|
|
this.Text = t;
|
2024-05-03 19:00:41 +02:00
|
|
|
|
|
|
|
this.GRID_SIZE = GRID_SIZE;
|
|
|
|
|
|
|
|
this.grid = grid;
|
|
|
|
|
|
|
|
this.row = i;
|
|
|
|
|
|
|
|
this.col = j;
|
2024-04-28 13:16:31 +02:00
|
|
|
}
|
2024-05-03 19:00:41 +02:00
|
|
|
|
2024-04-28 13:16:31 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void keyTyped(KeyEvent e) {
|
2024-04-29 15:08:14 +02:00
|
|
|
|
2024-05-03 19:00:41 +02:00
|
|
|
char chiffre = e.getKeyChar();
|
2024-04-28 13:16:31 +02:00
|
|
|
|
2024-04-29 15:08:14 +02:00
|
|
|
int taille = this.Text.getText().length();
|
2024-05-03 19:00:41 +02:00
|
|
|
|
2024-04-28 13:16:31 +02:00
|
|
|
|
2024-05-03 19:00:41 +02:00
|
|
|
if ( ((chiffre < '0') || (chiffre > '9')) && (chiffre != KeyEvent.VK_BACK_SPACE)) {
|
|
|
|
|
|
|
|
e.consume(); // ignorer l'événement
|
2024-04-28 13:16:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( taille >= 1 ) {
|
|
|
|
|
|
|
|
e.consume();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-05-03 19:00:41 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void keyPressed(KeyEvent e) {
|
|
|
|
|
|
|
|
char chiffre = e.getKeyChar();
|
|
|
|
|
|
|
|
if ( (chiffre > '0') || (chiffre < '9') && (chiffre != KeyEvent.VK_BACK_SPACE)) {
|
|
|
|
|
|
|
|
GrilleValide(chiffre);
|
|
|
|
}
|
2024-05-04 15:14:03 +02:00
|
|
|
if (!bad_numbers.isEmpty()) {
|
|
|
|
|
|
|
|
if ( chiffre == KeyEvent.VK_BACK_SPACE) {
|
2024-05-03 19:00:41 +02:00
|
|
|
|
2024-05-04 15:14:03 +02:00
|
|
|
Text.setBackground(Color.white);
|
2024-05-03 19:00:41 +02:00
|
|
|
|
2024-05-04 15:14:03 +02:00
|
|
|
while(!bad_numbers.isEmpty()) {
|
2024-05-03 19:00:41 +02:00
|
|
|
|
2024-05-04 15:14:03 +02:00
|
|
|
grid[bad_numbers.removeFirst()][bad_numbers.removeFirst()].setBackground(Color.white);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-05-03 19:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2024-05-04 15:14:03 +02:00
|
|
|
this.bad_numbers.add(row2);
|
|
|
|
this.bad_numbers.add(col);
|
|
|
|
|
2024-05-03 19:00:41 +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);
|
2024-05-04 15:14:03 +02:00
|
|
|
this.bad_numbers.add(row2);
|
|
|
|
this.bad_numbers.add(col);
|
2024-05-03 19:00:41 +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) {
|
|
|
|
this.grid[row][col2].setBackground(Color.red);
|
|
|
|
this.grid[row][col].setBackground(Color.red);
|
2024-05-04 15:14:03 +02:00
|
|
|
this.bad_numbers.add(row);
|
|
|
|
this.bad_numbers.add(col2);
|
2024-05-03 19:00:41 +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);
|
2024-05-04 15:14:03 +02:00
|
|
|
this.bad_numbers.add(row);
|
|
|
|
this.bad_numbers.add(col2);
|
2024-05-03 19:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vérifier la validité dans les régions
|
|
|
|
int rowregion = row/3*3;
|
|
|
|
int colregion = 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);
|
2024-05-04 15:14:03 +02:00
|
|
|
this.bad_numbers.add(row2);
|
|
|
|
this.bad_numbers.add(col2);
|
2024-05-03 19:00:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-28 13:16:31 +02:00
|
|
|
}
|
2024-05-03 19:00:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|