Ajout d'un système pour charger une grille de sudoku et l'afficher dans la grille
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -30,8 +31,8 @@ public class SudokuGrid extends JFrame {
|
|||||||
// Panneau pour les boutons
|
// Panneau pour les boutons
|
||||||
JPanel bouton = new JPanel();
|
JPanel bouton = new JPanel();
|
||||||
bouton.setBackground(new Color(0, 255, 0)); // Fond vert
|
bouton.setBackground(new Color(0, 255, 0)); // Fond vert
|
||||||
bouton.setPreferredSize(new Dimension(100, 0)); // Espace pour les bouton
|
bouton.setPreferredSize(new Dimension(100, 0)); // Espace pour les boutons
|
||||||
|
|
||||||
// Bouton pour sauvegarder la grille
|
// Bouton pour sauvegarder la grille
|
||||||
JButton save = new JButton("Sauvegarder");
|
JButton save = new JButton("Sauvegarder");
|
||||||
save.addActionListener(new ActionListener() {
|
save.addActionListener(new ActionListener() {
|
||||||
@@ -43,6 +44,16 @@ public class SudokuGrid extends JFrame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Bouton pour chargé la grille
|
||||||
|
JButton load = new JButton("Charger");
|
||||||
|
load.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
chargerFichier();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
bouton.add(load);
|
||||||
bouton.add(save);
|
bouton.add(save);
|
||||||
|
|
||||||
// Ajout des panneaux à la fenetre
|
// Ajout des panneaux à la fenetre
|
||||||
@@ -74,5 +85,45 @@ public class SudokuGrid extends JFrame {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// charger une grille existant
|
||||||
|
private void chargerFichier() {
|
||||||
|
JFileChooser choix = new JFileChooser(System.getProperty("user.dir")); // ce placer dans le repertoire ou se situe le code pour JFIleChooser
|
||||||
|
choix.setDialogTitle("Choisir un fichier");
|
||||||
|
choix.setFileFilter(new FileNameExtensionFilter("Fichiers texte", "txt"));
|
||||||
|
|
||||||
|
int choixValide = choix.showOpenDialog(this);
|
||||||
|
|
||||||
|
if (choixValide != JFileChooser.APPROVE_OPTION) return;
|
||||||
|
|
||||||
|
effacerGrille(); // efface que si l'utilisateur choisit un fichier
|
||||||
|
|
||||||
|
File fichierCharger = choix.getSelectedFile(); // permet de réecrire le fichier dans la grille
|
||||||
|
try (BufferedReader read = new BufferedReader(new FileReader(fichierCharger))) {
|
||||||
|
int a = 0;
|
||||||
|
String line;
|
||||||
|
while ((line = read.readLine()) != null && a < GRID_SIZE) {
|
||||||
|
for (int col = 0; col < Math.min(line.length(), GRID_SIZE); col++) {
|
||||||
|
char ch = line.charAt(col);
|
||||||
|
if (Character.isDigit(ch) && ch != '0') {
|
||||||
|
grid[a][col].setText(String.valueOf(ch));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a++;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
JOptionPane.showMessageDialog(null, "Erreur lors du chargement du fichier.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Effacer le contenu de la grille
|
||||||
|
private void effacerGrille() {
|
||||||
|
for (int i = 0; i < GRID_SIZE; i++) {
|
||||||
|
for (int j = 0; j < GRID_SIZE; j++) {
|
||||||
|
grid[i][j].setText("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user