2024-04-29 15:08:14 +02:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.event.*;
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SaveButton implements ActionListener {
|
|
|
|
|
|
|
|
private int GRID_SIZE;
|
|
|
|
private JTextField[][] grid;
|
|
|
|
|
|
|
|
public SaveButton(int GRID_SIZE, JTextField[][] grid ) {
|
|
|
|
|
|
|
|
this.GRID_SIZE = GRID_SIZE;
|
|
|
|
|
|
|
|
this.grid = grid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
2024-04-30 10:49:50 +02:00
|
|
|
saveFichier();
|
|
|
|
|
2024-04-29 15:08:14 +02:00
|
|
|
}
|
|
|
|
|
2024-04-30 10:49:50 +02:00
|
|
|
|
2024-05-01 15:05:23 +02:00
|
|
|
private void saveFichier() {
|
2024-04-29 15:08:14 +02:00
|
|
|
try {
|
2024-05-01 15:05:23 +02:00
|
|
|
FileOutputStream fr = new FileOutputStream("Test1.gri");
|
|
|
|
DataOutputStream fichier = new DataOutputStream(fr);
|
|
|
|
JTextField[][] texte = grid;
|
2024-04-29 15:08:14 +02:00
|
|
|
for (int i = 0; i < GRID_SIZE; i++) {
|
2024-05-01 15:05:23 +02:00
|
|
|
StringBuilder build = new StringBuilder();
|
2024-04-29 15:08:14 +02:00
|
|
|
for (int j = 0; j < GRID_SIZE; j++) {
|
2024-05-01 15:05:23 +02:00
|
|
|
String value = texte[i][j].getText();
|
|
|
|
if (value.isEmpty()) {
|
|
|
|
build.append("0");
|
2024-04-29 21:08:20 +02:00
|
|
|
} else {
|
2024-05-01 15:05:23 +02:00
|
|
|
build.append(value);
|
2024-04-29 21:08:20 +02:00
|
|
|
}
|
2024-04-29 15:08:14 +02:00
|
|
|
}
|
2024-05-01 15:05:23 +02:00
|
|
|
String convert = build.toString();
|
|
|
|
int write = Integer.parseInt(convert);
|
|
|
|
fichier.writeInt(write);
|
2024-04-29 21:08:20 +02:00
|
|
|
}
|
|
|
|
fichier.close();
|
2024-05-01 15:05:23 +02:00
|
|
|
JOptionPane.showMessageDialog(null, "Grille sauvegardée avec succès.", "Succès", JOptionPane.INFORMATION_MESSAGE);
|
2024-04-29 15:08:14 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
JOptionPane.showMessageDialog(null, "Erreur lors de la sauvegarde de la grille.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|