mikekine | Salut !
Dans le cadre d'un programme de sudoku, j'ai un souci avec les sauvegardes.
- Dès lors qu'une partie est terminée et la grille correctement remplie, un message apparait :
- Si la personne a résolu la grille dans un temps qui est parmi les 10 meilleurs, on demande son nom :
Là où se trouve mon problème, c'est que si l'utilisateur a précédement procédé à une sauvegarde (que ce soit cette partie là ou bien une autre), voilà ce que j'obtiens comme boite de dialogue :
Elle est bien là, mais il n'y plus rien dedans ...
Voici le code de ma classe Findejeu :
Code :
- public class Findejeu implements Observer,java.io.Serializable {
-
- private static final long serialVersionUID = -8647092533022968631L;
-
- private Pgrmsudoku pgrm;
- Saisienom saisienom;
-
- public Findejeu(Pgrmsudoku pgrm){
- this.pgrm = pgrm;
- pgrm.getJeu().getSudomodele().addObserver(this);
- saisienom = new Saisienom(pgrm, this);
- saisienom.pack();
- }
-
- public void updateObserver() {
- if (pgrm.getJeu().getSudomodele().getSequencefin()==1){
- if (pgrm.getJeu().getSudomodele().isGagne()==true) {
- JOptionPane.showMessageDialog(pgrm,"Félicitations !\nVous avez correctement rempli la grille\n","Fin du Jeu",JOptionPane.INFORMATION_MESSAGE);
- if (pgrm.getJeu().getSudomodele().isAidecalcul()==false){
- if (pgrm.getJeu().getSudomodele().isTopten()==false){
- JOptionPane.showMessageDialog(pgrm,"Désolé, vous ne figurez pas parmi les 10 meilleurs temps ...","Pas assez rapide !",JOptionPane.INFORMATION_MESSAGE);
- }
- else {
- saisienom.setLocationRelativeTo(pgrm);
- saisienom.setVisible(true);
- String s = saisienom.getValidatedText();
- if (s != null) {
- pgrm.getJeu().getSudomodele().setNomtemp(s);
- }
- }
- }
- }
- }
- }
- }
|
Voici celui de ma classe Saisienom :
Code :
- public class Saisienom extends JDialog implements ActionListener,PropertyChangeListener{
-
- private static final long serialVersionUID = 4566591878787558919L;
-
- private String typedText = null;
- private JTextField textField;
- private JOptionPane optionPane;
- private String btnString1 = "OK";
- private String btnString2 = "Annuler";
-
- public Saisienom(Pgrmsudoku pgrm, Findejeu parent) {
- super(pgrm, true);
- setTitle("Identité" );
- textField = new JTextField(10);
- String msgString1 = "Veuillez entrer votre nom :";
- Object[] array = {msgString1, textField};
- Object[] options = {btnString1, btnString2};
- optionPane = new JOptionPane(array,JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION, null, options, options[0]);
- setContentPane(optionPane);
- addComponentListener(new ComponentAdapter() {
- public void componentShown(ComponentEvent ce) {
- textField.requestFocusInWindow();
- }
- });
- textField.addActionListener(this);
- optionPane.addPropertyChangeListener(this);
- }
-
- public String getValidatedText() {
- return typedText;
- }
-
- public void actionPerformed(ActionEvent e) {
- optionPane.setValue(btnString1);
- }
-
- public void propertyChange(PropertyChangeEvent e) {
- int i=0;
- boolean test=true;
- String prop = e.getPropertyName();
- if (isVisible() && (e.getSource() == optionPane) && (JOptionPane.VALUE_PROPERTY.equals(prop) || JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
- Object value = optionPane.getValue();
- if (value == JOptionPane.UNINITIALIZED_VALUE) {
- return;
- }
- optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
- if (btnString1.equals(value)) {
- typedText = textField.getText();
- String ucText = typedText.toUpperCase();
- if (ucText.equals("" )) test=false;
- while (i<ucText.length() && test==true){
- if (ucText.charAt(i)<'A' || ucText.charAt(i)>'Z') test=false;
- i++;
- }
- if (test==true) clearAndHide();
- else {
- textField.selectAll();
- JOptionPane.showMessageDialog(Saisienom.this, "Désolé, \" " + typedText + " \" " + "n'est pas un nom valide.\n"
- + "Réessayez ! ", "Saisie incorrecte", JOptionPane.ERROR_MESSAGE);
- typedText = null;
- textField.requestFocusInWindow();
- }
- }
- else {
- typedText = "";
- clearAndHide();
- }
- }
- }
-
- public void clearAndHide() {
- textField.setText(null);
- setVisible(false);
- }
- }
|
Et enfin, celui de la classe sauvegarde :
Code :
- public void sauvegarde(){
- pgrm.getJeu().getSudomodele().setTempsinter(System.currentTimeMillis()-pgrm.getJeu().getSudomodele().getTempsdebut());
- try {
- FileOutputStream fichier = new FileOutputStream("sudomi.jeu" );
- ObjectOutputStream oos = new ObjectOutputStream(fichier);
- oos.writeObject(pgrm.getJeu().getSudomodele());
- oos.flush();
- oos.close();
- }
- catch (java.io.IOException e) {
- e.printStackTrace();
- }
- }
|
Est-ce que vous y comprenez qqch ? Message édité par mikekine le 27-05-2006 à 15:53:53
|