import java.awt.event.*; import javax.swing.*; import java.awt.Color; public class TextFilter extends KeyAdapter { private JTextField Text; private int GRID_SIZE; private JTextField[][] grid; private int row; private int col; private JTextField Text2; 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; } @Override 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 } 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 ( chiffre == KeyEvent.VK_BACK_SPACE) { if (!Text2.getText().isEmpty()) { Text.setBackground(Color.white); Text2.setBackground(Color.white); } } } 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.Text2 = grid[row2][col]; } } } 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.Text2 = grid[row2][col]; } } } // 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.Text2 = grid[row][col]; } } } 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.Text2 = grid[row][col2]; } } } // 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); this.Text2 = grid[row2][col2]; } } } } } } }