SAE21_2024/Sudoku/grille.java

162 lines
6.3 KiB
Java
Raw Normal View History

import javax.swing.*;
import java.awt.*;
2024-04-18 19:29:38 +02:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
2024-04-27 05:18:18 +02:00
import javax.swing.border.Border;
2024-04-18 19:29:38 +02:00
import javax.swing.text.*;
public class grille extends JComponent{
/*tableau de valeurs de la grille de sudoku*/
public static int[][] grid_values = null;
/*fonction pour afficher graphiquement la grille*/
public static void AfficherGrille (int[][] grille, boolean editable) {
/*paramètre de base de la fenetre*/
JFrame fenetre = new JFrame();
2024-04-22 15:38:29 +02:00
fenetre.setSize(900, 950);
/*fenetre.setResizable(false);*/
fenetre.setLocationRelativeTo(null);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2024-04-15 18:46:05 +02:00
/*Panneau pour la grille */
JPanel place_grille = new JPanel();
place_grille.setSize(900,900);
/*creation grille*/
2024-04-13 20:13:24 +02:00
GridLayout gestionnaire = new GridLayout(9,9,-2,-2);
2024-04-15 18:46:05 +02:00
place_grille.setLayout(gestionnaire);
if(editable){
JTextField[][] case_editable = null;
case_editable = new JTextField[9][9];
2024-04-27 05:18:18 +02:00
for (int ligne = 0; ligne < 9; ligne++) {
for (int col = 0; col < 9; col++) {
if (grille[ligne][col] == 0){
case_editable[ligne][col] = new JTextField("", 1);
2024-04-14 23:59:14 +02:00
}else{
2024-04-27 05:18:18 +02:00
case_editable[ligne][col] = new JTextField(String.valueOf(grille[ligne][col]), 1);
2024-04-14 23:59:14 +02:00
}
2024-04-27 05:18:18 +02:00
case_editable[ligne][col].setFont(new Font("Arial", Font.PLAIN, 30));
case_editable[ligne][col].setHorizontalAlignment(JTextField.CENTER);
if ((ligne % 3 == 0) && (ligne != 0) && (col % 3 == 0) && (col != 0)){
case_editable[ligne][col].setBorder(BorderFactory.createMatteBorder(5,5,2,2,Color.BLACK));
} else if((ligne % 3 == 0) && (ligne != 0)){
case_editable[ligne][col].setBorder(BorderFactory.createMatteBorder(5,2,2,2,Color.BLACK));
} else if ((col % 3 == 0) && (col != 0)){
case_editable[ligne][col].setBorder(BorderFactory.createMatteBorder(2,5,2,2,Color.BLACK));
} else {
case_editable[ligne][col].setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
}
place_grille.add(case_editable[ligne][col]);
}
}
2024-04-27 05:18:18 +02:00
} else {
2024-04-14 23:59:14 +02:00
/*affichage de la grille*/
JTextField[][] case_modifiable = null;
JLabel[][] case_depart = null;
case_depart = new JLabel[9][9];
case_modifiable = new JTextField[9][9];
2024-04-27 05:18:18 +02:00
for (int ligne = 0; ligne < 9; ligne++) {
for (int col = 0; col < 9; col++) {
if ((grid_values[ligne][col]) == 0) {
case_modifiable[ligne][col] = new JTextField("", 1);
case_modifiable[ligne][col].setFont(new Font("Arial", Font.PLAIN, 30));
case_modifiable[ligne][col].setHorizontalAlignment(JTextField.CENTER);
if ((ligne % 3 == 0) && (ligne != 0)){
case_modifiable[ligne][col].setBorder(BorderFactory.createMatteBorder(5,2,2,2,Color.BLACK));
} else if ((col % 3 == 0) && (col != 0)){
case_modifiable[ligne][col].setBorder(BorderFactory.createMatteBorder(2,5,2,2,Color.BLACK));
} else if ((ligne % 3 == 0) && (ligne != 0) && (col % 3 == 0) && (col != 0)){
case_modifiable[ligne][col].setBorder(BorderFactory.createMatteBorder(5,5,2,2,Color.BLACK));
}else {
case_modifiable[ligne][col].setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
}
place_grille.add(case_modifiable[ligne][col]);
2024-04-14 23:59:14 +02:00
} else {
2024-04-27 05:18:18 +02:00
case_depart[ligne][col] = new JLabel(String.valueOf(grid_values[ligne][col]));
case_depart[ligne][col].setFont(new Font("Arial", Font.PLAIN, 30));
case_depart[ligne][col].setHorizontalAlignment(JTextField.CENTER);
if ((ligne % 3 == 0) && (ligne != 0)){
case_depart[ligne][col].setBorder(BorderFactory.createMatteBorder(5,2,2,2,Color.BLACK));
2024-04-27 05:18:18 +02:00
} else if ((col % 3 == 0) && (col != 0)){
case_depart[ligne][col].setBorder(BorderFactory.createMatteBorder(2,5,2,2,Color.BLACK));
2024-04-27 05:18:18 +02:00
} else if ((ligne % 3 == 0) && (ligne != 0) && (col % 3 == 0) && (col != 0)){
case_depart[ligne][col].setBorder(BorderFactory.createMatteBorder(5,5,2,2,Color.BLACK));
2024-04-27 05:18:18 +02:00
} else {
case_depart[ligne][col].setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLACK));
2024-04-27 05:18:18 +02:00
}
place_grille.add(case_depart[ligne][col]);
2024-04-14 23:59:14 +02:00
}
}
}
}
2024-04-26 16:26:26 +02:00
/*bouton(s) grille(s)*/
2024-04-15 18:46:05 +02:00
JPanel bouton_grille = new JPanel();
2024-04-26 16:26:26 +02:00
bouton_grille.setSize(300,200);
2024-04-18 19:29:38 +02:00
bouton_grille.setLayout(new BorderLayout());
2024-04-15 18:46:05 +02:00
JButton verifier = new JButton("verifier");
bouton_grille.add(verifier);
fenetre.add(bouton_grille,BorderLayout.SOUTH);
2024-04-18 19:29:38 +02:00
fenetre.add(place_grille, BorderLayout.CENTER);
2024-04-26 16:26:26 +02:00
/*affichage fenetre*/
fenetre.setVisible(true);
2024-04-14 23:59:14 +02:00
/*System.out.println(grid[0][0].getText());*/
2024-04-18 19:29:38 +02:00
verifier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent verifier) {
}
});
}
/*fonction pour passer d'un fichier.gri à un tableau de valeur*/
public static int[][] ChargerGrille(String cheminFichier){
try {
FileInputStream fs = new FileInputStream(cheminFichier);
DataInputStream fichier = new DataInputStream(fs);
grid_values = new int[9][9];
String string_values = "";
int index = 0;
for (int a = 0; a < 9 ; a++ ) {
String ligne = String.valueOf(fichier.readInt());
while (ligne.length() < 9){
ligne = "0" + ligne;
}
string_values = string_values + ligne;
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
grid_values[i][j] = Character.getNumericValue(string_values.charAt(index));
index++;
}
}
try {
fs.close();
return grid_values;
}catch(IOException e){
System.err.println("erreur fermeture du fichier");
}
}catch(IOException e) {
System.err.println("erreur ouverture du fichier");
}
return null;
}
2024-04-26 16:26:26 +02:00
public static void ExporterGrille(int[][] grille){
}
}