Chargement d'une grille non modifiable pour le deuxieme programme
This commit is contained in:
parent
b0357b3385
commit
5d88f81b49
@ -1,6 +1,7 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
import java.awt.event.*;
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
|
||||
public class LoadButton implements ActionListener {
|
||||
@ -18,7 +19,11 @@ public class LoadButton implements ActionListener {
|
||||
@Override
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
chargerFichier();
|
||||
if (SudokuGrid.BOOL) {
|
||||
chargerFichier();
|
||||
} else {
|
||||
chargerFichierNonEditable();
|
||||
}
|
||||
}
|
||||
|
||||
private void chargerFichier() {
|
||||
@ -56,6 +61,46 @@ public class LoadButton implements ActionListener {
|
||||
}
|
||||
}
|
||||
|
||||
private void chargerFichierNonEditable() {
|
||||
JFileChooser choix = new JFileChooser(System.getProperty("user.dir"));
|
||||
choix.setDialogTitle("Choisir un fichier");
|
||||
choix.setFileFilter(new FileNameExtensionFilter("Fichiers gri", "gri"));
|
||||
int choixValide = choix.showOpenDialog(parent);
|
||||
|
||||
if (choixValide != JFileChooser.APPROVE_OPTION) return;
|
||||
File Selection = choix.getSelectedFile();
|
||||
|
||||
effacerGrille();
|
||||
try {
|
||||
FileInputStream fich = new FileInputStream(Selection);
|
||||
DataInputStream file = new DataInputStream(fich);
|
||||
for (int i = 0; i < 9; i++) {
|
||||
int valeur = file.readInt();
|
||||
for (int j = 8; j >= 0; j--) {
|
||||
int cell = valeur % 10;
|
||||
valeur /= 10;
|
||||
grid[i][j].setText(cell == 0 ? "" : String.valueOf(cell));
|
||||
// Rendre les champs de texte non éditables
|
||||
if (cell != 0) {
|
||||
grid[i][j].setEditable(false);
|
||||
grid[i][j].setBackground(Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
file.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Erreur de fermeture");
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
JOptionPane.showMessageDialog(null, "Le fichier sélectionné est introuvable.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
JOptionPane.showMessageDialog(null, "Erreur lors de l'ouverture du fichier.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Effacer le contenu de la grille
|
||||
public void effacerGrille() {
|
||||
for (int i = 0; i < GRID_SIZE; i++) {
|
||||
|
@ -4,6 +4,7 @@ import java.awt.*;
|
||||
public class SudokuGrid extends JFrame {
|
||||
private static final int GRID_SIZE = 9; // Taille de la grille Sudoku 9x9
|
||||
private static final int REGION_SIZE = 3;
|
||||
public static final boolean BOOL = true;
|
||||
private JTextField[][] grid;
|
||||
|
||||
public SudokuGrid() {
|
||||
@ -47,13 +48,13 @@ public class SudokuGrid extends JFrame {
|
||||
|
||||
// Panneau pour les boutons
|
||||
JPanel bouton = new JPanel();
|
||||
bouton.setBackground(new Color(88, 169, 191)); // Fond vert
|
||||
bouton.setBackground(new Color(88, 169, 191));
|
||||
bouton.setPreferredSize(new Dimension(150, 0)); // Espace pour les boutons
|
||||
|
||||
// Bouton pour sauvegarder la grille
|
||||
JButton save = new JButton("Sauvegarder");
|
||||
|
||||
SaveButton saver = new SaveButton(GRID_SIZE,grid);
|
||||
SaveButton saver = new SaveButton(GRID_SIZE,grid);
|
||||
|
||||
save.addActionListener(saver);
|
||||
|
||||
@ -61,7 +62,7 @@ public class SudokuGrid extends JFrame {
|
||||
// Bouton pour chargé la grille
|
||||
JButton load = new JButton("Charger");
|
||||
|
||||
LoadButton loader = new LoadButton(GRID_SIZE, grid);
|
||||
LoadButton loader = new LoadButton(GRID_SIZE, grid);
|
||||
|
||||
load.addActionListener(loader);
|
||||
|
||||
|
83
SudokuGridTesteur.java
Normal file
83
SudokuGridTesteur.java
Normal file
@ -0,0 +1,83 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class SudokuGridTesteur extends JFrame {
|
||||
private static final int GRID_SIZE = 9; // Taille de la grille Sudoku 9x9
|
||||
private static final int REGION_SIZE = 3;
|
||||
public static final boolean BOOL = false;
|
||||
private JTextField[][] grid;
|
||||
|
||||
public SudokuGridTesteur() {
|
||||
// Panneau pour la grille Sudoku.
|
||||
JPanel gridPanel = new JPanel();
|
||||
gridPanel.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); // Utiliser GridLayout
|
||||
gridPanel.setBackground(new Color(88, 169, 191)); // Fond vert
|
||||
|
||||
// Initialiser la grille
|
||||
grid = new JTextField[GRID_SIZE][GRID_SIZE];
|
||||
for (int i = 0; i < GRID_SIZE; i++) {
|
||||
for (int j = 0; j < GRID_SIZE; j++) {
|
||||
grid[i][j] = new JTextField();
|
||||
|
||||
TextFilter filtre = new TextFilter(grid[i][j], GRID_SIZE, grid, i, j);
|
||||
|
||||
grid[i][j].addKeyListener(filtre);
|
||||
grid[i][j].setHorizontalAlignment(JTextField.CENTER);
|
||||
grid[i][j].setFont(new Font("Verdana", Font.BOLD,40));
|
||||
gridPanel.add(grid[i][j]);
|
||||
|
||||
int top = 1;
|
||||
int left = 1;
|
||||
int bottom = 1;
|
||||
int right = 1;
|
||||
|
||||
// Vérifier si la case est sur le bord de la région horizontalement
|
||||
if ((j + 1) % REGION_SIZE == 0 && j != GRID_SIZE - 1) {
|
||||
right = 5; // Ajouter une bordure plus épaisse à droite
|
||||
}
|
||||
|
||||
// Vérifier si la case est sur le bord de la région verticalement
|
||||
if ((i + 1) % REGION_SIZE == 0 && i != GRID_SIZE - 1) {
|
||||
bottom = 5; // Ajouter une bordure plus épaisse en bas
|
||||
}
|
||||
|
||||
// Appliquer la bordure à la case
|
||||
grid[i][j].setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, Color.BLACK));
|
||||
}
|
||||
}
|
||||
|
||||
// Panneau pour les boutons
|
||||
JPanel bouton = new JPanel();
|
||||
bouton.setBackground(new Color(88, 169, 191));
|
||||
bouton.setPreferredSize(new Dimension(150, 0)); // Espace pour les boutons
|
||||
|
||||
// Bouton pour sauvegarder la grille
|
||||
JButton save = new JButton("Sauvegarder");
|
||||
|
||||
SaveButton saver = new SaveButton(GRID_SIZE,grid);
|
||||
|
||||
save.addActionListener(saver);
|
||||
|
||||
|
||||
// Bouton pour chargé la grille
|
||||
JButton load = new JButton("Charger");
|
||||
|
||||
LoadButton loader = new LoadButton(GRID_SIZE, grid);
|
||||
|
||||
load.addActionListener(loader);
|
||||
|
||||
|
||||
|
||||
|
||||
bouton.add(load);
|
||||
bouton.add(save);
|
||||
|
||||
|
||||
// Ajout des panneaux à la fenetre
|
||||
getContentPane().add(gridPanel, BorderLayout.CENTER);
|
||||
getContentPane().add(bouton, BorderLayout.EAST);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user