From 8db83dcc2dc3a55566b401ae8a839c80e468d047 Mon Sep 17 00:00:00 2001 From: follea Date: Sun, 28 Apr 2024 21:13:37 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20syst=C3=A8me=20pour=20charger=20?= =?UTF-8?q?une=20grille=20de=20sudoku=20et=20l'afficher=20dans=20la=20gril?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SudokuGrid.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/SudokuGrid.java b/SudokuGrid.java index 75d917a..e221500 100644 --- a/SudokuGrid.java +++ b/SudokuGrid.java @@ -1,4 +1,5 @@ import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.awt.event.*; import java.io.*; @@ -30,8 +31,8 @@ public class SudokuGrid extends JFrame { // Panneau pour les boutons JPanel bouton = new JPanel(); 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 JButton save = new JButton("Sauvegarder"); 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); // Ajout des panneaux à la fenetre @@ -74,5 +85,45 @@ public class SudokuGrid extends JFrame { 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(""); + } + } + } }