java bestand opslaan openen

Status
Niet open voor verdere reacties.

vlWim

Nieuwe gebruiker
Lid geworden
27 mei 2008
Berichten
2
Hallo,

Ik ben bezig aan een projectje om vier op een rij te programmeren.

Het doel is om een huidig spel te kunnen bewaren en terug openen.
Telkens ik een oude spelsituatie terug open werkt mijn paint niet meer
en wordt het bord niet meer getekend. Ik denk dat ik de klasse
juist terug open maar dat er iets met mijn repaint fout ga.

Het is java - swing.
De code zit in bijgevoegd bestand.
Weet iemand een waar het probleem zit?

Alvast bedankt

http://users.skynet.be/fa311618/Main.java
 
Je hebt alles in 1 bestand staan?

Als ik jou was, dan zou ik alle hoofdclasses in een aparte classfile zetten.
Houdt het overzichtelijk.

Dus een aparte classfile voor: Main, bediening en spelbord

Als je van de String[][] schijven een int[][] maakt is je probleem opgelost.

Alles wat ik rood gemarkeert heb, heb ik aangepast.(kan er een paar gemist hebben)
De static final ints gebruik ik om het vergelijken makkelijk te maken. Een string vergelijken met een andere string middels een == is niet aan te raden omdat het problemen op kan leveren. Zet per ongeluk 1 hoofdletter verkeerd ;)

class Main:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Main extends JFrame {

    private Spelbord nieuwSpel;
    private Bediening bediening;
    private JMenuBar menuBar;
    private JMenu menuBestand,  menuHelp;
    private JMenuItem itemHerbegin,  itemBewaar,  itemOpen,  itemSluit,  itemHelp;
    private String directory,  filename;

    public Main() {

        Container contentpane = getContentPane();

        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 380);
        nieuwSpel = new Spelbord();
        bediening = new Bediening(nieuwSpel);

        contentpane.setLayout(new BorderLayout());
        contentpane.add(bediening, BorderLayout.NORTH);
        contentpane.add(nieuwSpel, BorderLayout.CENTER);


        menuBar = new JMenuBar();

        menuBestand = new JMenu("Bestand");
        menuHelp = new JMenu("Help");

        itemHerbegin = new JMenuItem("Nieuw Spel");
        itemBewaar = new JMenuItem("Bewaar");
        itemOpen = new JMenuItem("Open");
        itemSluit = new JMenuItem("Sluit");
        itemHelp = new JMenuItem("Help");
        itemHerbegin.addActionListener(new MenuHandler());
        itemBewaar.addActionListener(new MenuHandler());
        itemOpen.addActionListener(new MenuHandler());
        itemSluit.addActionListener(new MenuHandler());
        itemHelp.addActionListener(new MenuHandler());
        menuBar.add(menuBestand);
        menuBar.add(menuHelp);
        menuBestand.add(itemHerbegin);
        menuBestand.add(itemBewaar);
        menuBestand.add(itemOpen);
        menuBestand.add(itemSluit);
        menuHelp.add(itemHelp);

        setJMenuBar(menuBar);
        setTitle("Vier op een rij");
        setVisible(true);
        setResizable(false);
    }

    class MenuHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            if (e.getSource() == itemHerbegin) {


                nieuwSpel.herbegin();

            }
            if (e.getSource() == itemBewaar) {


                FileDialog fileDialog2 = new FileDialog(Main.this, "Bewaar een bestand", FileDialog.SAVE);
                fileDialog2.setVisible(true);
                directory = fileDialog2.getDirectory();
                filename = fileDialog2.getFile();
                try {
                    ObjectOutputStream uit = new ObjectOutputStream(new FileOutputStream(directory + filename));
                    uit.writeObject(nieuwSpel.schijven);
                    uit.close();

                } catch (IOException eo) {
                    System.out.println("fout");
                }
            }
            if (e.getSource() == itemOpen) {

[COLOR="Red"]                int[][] schijven;
                schijven = new int[7][6];[/COLOR]

                FileDialog fileDialog = new FileDialog(Main.this, "Open een bestand", FileDialog.LOAD);
                fileDialog.setVisible(true);
                directory = fileDialog.getDirectory();
                filename = fileDialog.getFile();

                try {
                    ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                            directory + filename));
                    try {
                        schijven = (int[][]) in.readObject();
                        nieuwSpel.setSpel(schijven);

                    } catch (ClassNotFoundException iou) {
                        System.out.println("jj");

                    }
                } catch (IOException iot) {
                    System.out.println("jj");
                }

            }


            if (e.getSource() == itemSluit) {
                dispose();
                System.exit(0);


            }
            if (e.getSource() == itemHelp) {
                help();

            }

        }
    }

    private void jbInit() throws Exception {
    }

    private void help() {

        JOptionPane.showMessageDialog(this, "De bedoeling is om zo snel mogelijk vier op een rij te vormen.\nDruk op de knop boven de rij om een schijf te laten vallen.\n Druk op herbegin om een nieuw spel te beginnen.", "Help!", JOptionPane.INFORMATION_MESSAGE);


    }

    public static void main(String[] args) {
        new Main();
    }
}

Class Spelbord:

Code:
import java.awt.*;
import java.io.Serializable;
import javax.swing.*;

/**
 *
 * @author 
 */
class Spelbord extends JPanel implements Serializable {

[COLOR="Red"]    int[][] schijven;[/COLOR]
    private int j,  i,  dropKolom,  huidigepos,  roodaantalrechts,  roodaantallinks,  geelaantalrechts,  geelaantallinks;
    private boolean beurt;
    private int[] ypos;
[COLOR="Red"]    private int welkeKleur;[/COLOR]
    private int winnaar;
    private boolean aandebeurt = false;
[COLOR="Red"]    private static final int LEEG = 0;
    private static final int ROOD = 1;
    private static final int GEEL = 2;[/COLOR]

    public Spelbord() {

 [COLOR="Red"]       schijven = new int[7][6];[/COLOR]
        for (int i = 0; i <= 6; i++) {

            for (int j = 0; j <= 5; j++) {
                schijven[i][j] = [COLOR="Red"]LEEG[/COLOR];

            }
        }
        ypos = new int[7];
        for (int k = 0; k <= 6; k++) {

            ypos[k] = 5;

        }

    }

    public void setSpel([COLOR="Red"]int[][] schijven[/COLOR]) {
        this.schijven = schijven;
        repaint();

    }

    public void herbegin() {
        for (int j = 0; j <= 6; j++) {

            for (int i = 0; i <= 5; i++) {
                schijven[j][i] = [COLOR="Red"]LEEG[/COLOR];

            }
        }
        for (int i = 0; i <= 6; i++) {

            ypos[i] = 5;

        }

        repaint();
        roodaantalrechts = 0;
        roodaantallinks = 0;
        geelaantalrechts = 0;
        geelaantallinks = 0;
        winnaar = 0;

        aandebeurt = false;

    }

    public boolean getBeurt() {


        return aandebeurt;
    }

    public void setPion(int kolom) {
        dropKolom = kolom;




        huidigepos = ypos[kolom];
        if (huidigepos >= 0) {

            aandebeurt = !aandebeurt;
            if (schijven[kolom][ypos[kolom]] == [COLOR="Red"]LEEG[/COLOR]) {
                if (aandebeurt) {
                    schijven[kolom][ypos[kolom]] = [COLOR="Red"]GEEL[/COLOR];

                    ypos[kolom] = huidigepos - 1;
                } else {
                    schijven[kolom][ypos[kolom]] = [COLOR="Red"]ROOD[/COLOR];
                    ypos[kolom] = huidigepos - 1;
                }
            } else {
                ypos[kolom] = huidigepos - 1;
            }

            repaint();
            //diagonaal controleren op vier op een rij
            // met rechts en links worden de tegenovergestelde diagonalen bedoeld
            welkeKleur = [COLOR="Red"]GEEL[/COLOR];
            if ((schijven[dropKolom][huidigepos] == [COLOR="Red"]ROOD[/COLOR])) {

                roodaantalrechts = 1;
                roodaantallinks = 1;
                geelaantalrechts = 0;
                geelaantallinks = 0;


            } else {


                roodaantalrechts = 0;
                roodaantallinks = 0;
                geelaantalrechts = 1;
                geelaantallinks = 1;

            }
            for (int i = 0; i <= 1; i++) {


                for (int j = 1; j <= 4; j++) {


                    if ((dropKolom + j <= 6) && (huidigepos + j <= 5)) {
                        if ((schijven[dropKolom + j][huidigepos + j] == welkeKleur)) {
                            if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                roodaantalrechts = roodaantalrechts + 1;
                            } else {
                                geelaantalrechts = geelaantalrechts + 1;
                            }
                        }
                    }

                    if ((dropKolom - j >= 0) && (huidigepos + j <= 5)) {
                        if ((schijven[dropKolom - j][huidigepos + j] == welkeKleur)) {
                            if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                roodaantallinks = roodaantallinks + 1;
                            } else {
                                geelaantallinks = geelaantallinks + 1;
                            }
                        }
                    }
                    if ((dropKolom + j <= 6) && (huidigepos - j >= 0)) {
                        if ((schijven[dropKolom + j][huidigepos - j] == welkeKleur)) {
                            if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                roodaantallinks = roodaantallinks + 1;
                            } else {
                                geelaantallinks = geelaantallinks + 1;
                            }
                        }
                    }
                    if ((dropKolom - j >= 0) && (huidigepos - j >= 0)) {
                        if ((schijven[dropKolom - j][huidigepos - j] == welkeKleur)) {
                            if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                roodaantalrechts = roodaantalrechts + 1;
                            } else {
                                geelaantalrechts = geelaantalrechts + 1;
                            }
                        }
                    }
                }
                welkeKleur = [COLOR="Red"]ROOD[/COLOR];
            }

            //System.out.println(schijven[1][3]); 
            // System.out.println(roodaantalrechts); 
            // System.out.println(geelaantalrechts);
            // System.out.println(geelaantallinks);

            if ((roodaantallinks >= 4) || roodaantalrechts >= 4) {
                winnaar = [COLOR="Red"]ROOD[/COLOR];
            } else if (geelaantallinks >= 4 || geelaantalrechts >= 4) {
                winnaar = [COLOR="Red"]GEEL[/COLOR];
            }



            if ((schijven[dropKolom][huidigepos] == [COLOR="Red"]ROOD[/COLOR])) {

                roodaantalrechts = 1;
                roodaantallinks = 1;
                geelaantalrechts = 0;
                geelaantallinks = 0;


            } else {


                roodaantalrechts = 0;
                roodaantallinks = 0;
                geelaantalrechts = 1;
                geelaantallinks = 1;

            }

            //horizontaal
            welkeKleur = [COLOR="Red"]GEEL[/COLOR];
            for (int i = 0; i <= 1; i++) {


                for (int j = 1; j <= 3; j++) {

                    if ((schijven[dropKolom][huidigepos] == welkeKleur)) {
                        if ((dropKolom + j <= 6)) {
                            if ((schijven[dropKolom + j][huidigepos] == welkeKleur)) {
                                if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                    roodaantalrechts = roodaantalrechts + 1;
                                } else {
                                    geelaantalrechts = geelaantalrechts + 1;
                                }
                            }
                        }
                    }

                    if ((dropKolom - j >= 0)) {
                        if ((schijven[dropKolom - j][huidigepos] == welkeKleur)) {
                            if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                roodaantallinks = roodaantallinks + 1;
                            } else {
                                geelaantallinks = geelaantallinks + 1;
                            }
                        }
                    }

                }
                welkeKleur = [COLOR="Red"]ROOD[/COLOR];
            }
            if ((roodaantallinks >= 4) || roodaantalrechts >= 4) {
                winnaar = [COLOR="Red"]ROOD[/COLOR];
            } else if (geelaantallinks >= 4 || geelaantalrechts >= 4) {
                winnaar = [COLOR="Red"]GEEL[/COLOR];
            }
            if ((schijven[dropKolom][huidigepos] == [COLOR="Red"]ROOD[/COLOR])) {

                roodaantalrechts = 1;
                roodaantallinks = 1;
                geelaantalrechts = 0;
                geelaantallinks = 0;


            } else {


                roodaantalrechts = 0;
                roodaantallinks = 0;
                geelaantalrechts = 1;
                geelaantallinks = 1;

            }

            //verticaal
            welkeKleur = [COLOR="Red"]GEEL[/COLOR];
            for (int i = 0; i <= 1; i++) {


                for (int j = 1; j <= 3; j++) {

                    if ((schijven[dropKolom][huidigepos] == welkeKleur)) {
                        if ((huidigepos + j <= 5)) {
                            if ((schijven[dropKolom][huidigepos + j] == welkeKleur)) {
                                if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                    roodaantalrechts = roodaantalrechts + 1;
                                } else {
                                    geelaantalrechts = geelaantalrechts + 1;
                                }
                            }
                        }
                    }

                    if ((huidigepos - j >= 0)) {
                        if ((schijven[dropKolom][huidigepos - j] == welkeKleur)) {
                            if (welkeKleur == [COLOR="Red"]ROOD[/COLOR]) {
                                roodaantalrechts = roodaantalrechts + 1;
                            } else {
                                geelaantalrechts = geelaantalrechts + 1;
                            }
                        }
                    }

                }
                welkeKleur = [COLOR="Red"]ROOD[/COLOR];
            }
            if ((roodaantallinks >= 4) || roodaantalrechts >= 4) {
                winnaar = [COLOR="Red"]ROOD[/COLOR];
            } else if (geelaantallinks >= 4 || geelaantalrechts >= 4) {
                winnaar = [COLOR="Red"]GEEL[/COLOR];
            }

        }
    }

    public int getWinnaar() {

        return winnaar;

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, 290, 260);

        for (int j = 0; j <= 6; j++) {

            for (int i = 0; i <= 5; i++) {


                if (schijven[j][i] == [COLOR="Red"]LEEG[/COLOR]) {
                    g.setColor(Color.WHITE);
                    g.fillOval(10 + (j * 40), 10 + (i * 40), 30, 30);
                    System.out.println(schijven[j][i]);

                } else if (schijven[j][i] == [COLOR="Red"]GEEL[/COLOR]) {
                    g.setColor(Color.YELLOW);
                    g.fillOval(10 + (j * 40), 10 + (i * 40), 30, 30);

                } else if (schijven[j][i] == [COLOR="Red"]ROOD[/COLOR]) {
                    g.setColor(Color.RED);
                    g.fillOval(10 + (j * 40), 10 + (i * 40), 30, 30);

                }
            }
        }
    }
}

Class Bediening:

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author
 */
class Bediening extends JPanel {

    private Spelbord nieuwSpel;
    private JButton[] knoppen;
    private JLabel aanDeBeurt,  aanDeBeurt2,  gewonnen;
[COLOR="Red"]    private int woord,  kleurWinnaar;[/COLOR]
    private JPanel ok,  ok2;
[COLOR="Red"]    private int winnaar;[/COLOR]
    private JButton reset;
    
[COLOR="Red"]    private static final int LEEG = 0;
    private static final int ROOD = 1;
    private static final int GEEL = 2;[/COLOR]

    public Bediening(Spelbord nieuwSpel) {

        setLayout(new BorderLayout());
        knoppen = new JButton[7];
        aanDeBeurt = new JLabel("Aan de beurt: ");
        aanDeBeurt2 = new JLabel(" Speler 1 (geel).");
        gewonnen = new JLabel("");
        this.nieuwSpel = nieuwSpel;
        ok = new JPanel();
        ok2 = new JPanel();
        reset = new JButton("Herbegin spel.");
        reset.addActionListener(new Knophandler(7));

        ok.setLayout(new GridLayout(1, 7));
        ok2.setLayout(new GridLayout(1, 2));


        for (int j = 0; j <= 6; j++) {

            knoppen[j] = new JButton("" + (j + 1));
            knoppen[j].addActionListener(new Knophandler(j));
            knoppen[j].setSize(30, 10);
            ok.add(knoppen[j]);


        }
        ok2.add(reset);
        ok2.add(gewonnen);




        add(aanDeBeurt, BorderLayout.WEST);
        add(aanDeBeurt2, BorderLayout.CENTER);
        add(ok2, BorderLayout.NORTH);
        add(ok, BorderLayout.SOUTH);

    }

    class Knophandler implements ActionListener {

        private int j; //j = knopje waaraan de handler hangt

        public Knophandler(int j) {
            this.j = j;
        }

        public void actionPerformed(ActionEvent e) {

            if (j < 7) {

                nieuwSpel.setPion(j);

                if (nieuwSpel.getBeurt() == false) {
                    aanDeBeurt2.setText(" Speler 1(geel).");

                } else {
                    aanDeBeurt2.setText(" Speler 2(rood).");
                }

            } else {

                nieuwSpel.herbegin();
                for (int j = 0; j <= 6; j++) {

                    knoppen[j].setEnabled(true);

                }
                aanDeBeurt2.setText(" Speler 1(geel).");
                gewonnen.setText("");

            }

            winnaar = nieuwSpel.getWinnaar();
            if (winnaar == [COLOR="Red"]ROOD[/COLOR]) {

                gewonnen.setText("Speler 2 (rood) wint.");
                aanDeBeurt2.setText("Herbegin spel.");
                for (int j = 0; j <= 6; j++) {

                    knoppen[j].setEnabled(false);

                }

            } else if (winnaar == [COLOR="Red"]GEEL[/COLOR]) {

                gewonnen.setText("Speler 1 (geel) wint.");
                aanDeBeurt2.setText("Herbegin spel.");
                for (int j = 0; j <= 6; j++) {

                    knoppen[j].setEnabled(false);

                }

            }


        }
    }
}
 
Laatst bewerkt:
Opgelost

Je bent fantastisch,probleem is opgelost!
Meteen weeral wat bijgeleerd.


Bedankt voor de hulp en de tips.

Groet Wim
 
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan