mise à jour du textfilter

This commit is contained in:
2024-05-04 16:51:35 +02:00
parent b4b7614832
commit b0357b3385

View File

@@ -7,28 +7,19 @@ import java.util.ArrayList;
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>();
public TextFilter (JTextField t,int GRID_SIZE, JTextField[][] grid, int i, int j) {
this.Text = t;
this.GRID_SIZE = GRID_SIZE;
this.grid = grid;
this.row = i;
this.col = j;
}
@@ -38,48 +29,38 @@ public class TextFilter extends KeyAdapter {
public void keyTyped(KeyEvent e) {
char chiffre = e.getKeyChar();
int taille = this.Text.getText().length();
if ( ((chiffre < '0') || (chiffre > '9')) && (chiffre != KeyEvent.VK_BACK_SPACE)) {
e.consume(); // ignorer l'événement
e.consume(); // ignorer l'événement
}
if ( ((chiffre >= '0') && (chiffre <= '9')) && (chiffre != KeyEvent.VK_ENTER)) {
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);
}
}
if ( (bad_numbers.isEmpty()) && (chiffre == KeyEvent.VK_BACK_SPACE)) {
Coloriage();
}
if ( taille >= 1 ) {
e.consume();
}
}
@Override
public void keyPressed(KeyEvent e) {
char chiffre = e.getKeyChar();
if ( (chiffre > '0') || (chiffre < '9') && (chiffre != KeyEvent.VK_BACK_SPACE)) {
GrilleValide(chiffre);
}
if (!bad_numbers.isEmpty()) {
if ( chiffre == KeyEvent.VK_BACK_SPACE) {
Text.setBackground(Color.white);
while(!bad_numbers.isEmpty()) {
grid[bad_numbers.removeFirst()][bad_numbers.removeFirst()].setBackground(Color.white);
}
}
}
}
public void GrilleValide(char chiffre) {
@@ -141,8 +122,8 @@ public class TextFilter extends KeyAdapter {
}
// Vérifier la validité dans les régions
int rowregion = row/3*3;
int colregion = col/3*3;
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++) {
@@ -163,6 +144,40 @@ public class TextFilter extends KeyAdapter {
}
}
}
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);
}
}
}
}
}