java - Sudoku game, serialization issue -


i creating sudoku game , trying provide options save, save as, , open games. using jfilechooser this. able save (or "save as") when try open saved file, error. new programming , i'm hoping spot issue , educate me on how read in contents of sudoku board when saving (as how deal re-creating sudoku board when open file). hear there easier way deal using inputstream/outputstream instead of reader/writer...

here code inner class implements (i don't know if there's way post entire class without exceeding character limit text box.):

  // inner class provides jmenubar object @ top of   // board   class menuattop extends jmenubar implements actionlistener{      // sudokumain2 object dealing     private sudokumain2 main;      // "file" menu     private jmenu filemenu;     // "new game" option     private jmenuitem newgame;     // "open" option     private jmenuitem open;     // "save" option     private jmenuitem save;     // "save as" option     private jmenuitem saveas;     // "reset" option     private jmenuitem reset;     // "quit" option     private jmenuitem quit;      // ability choose files     private jfilechooser choose;      // saved file //    // compiler not allow "static" keyword     private file filesaved = null;      private object opener;      // jdialog object create dialog box prompt     // user new game information     private jdialog createnewwin;       /**      * constructs menuattop object.      *       * @param m sudokumain2 object referred to.      */     public menuattop(final sudokumain2 m) {        main = m;        opener = null;       choose = new jfilechooser();        // instantiate , bind reference       filemenu = new jmenu("file");       add(filemenu);        // instantiate , bind reference       newgame = new jmenuitem("new game");       newgame.setaccelerator(keystroke.getkeystroke(keyevent.vk_n,                                                     actionevent.ctrl_mask));       filemenu.add(newgame);       newgame.addactionlistener(this);        open = new jmenuitem("open");       open.setaccelerator(keystroke.getkeystroke(keyevent.vk_o,                                                  actionevent.ctrl_mask));       filemenu.add(open);       // add action listener "open" option       open.addactionlistener(this);        save = new jmenuitem("save");       save.setaccelerator(keystroke.getkeystroke(keyevent.vk_s,                                                  actionevent.ctrl_mask));       filemenu.add(save); //      //save.setenabled(false);       // add action listener "save" option       save.addactionlistener(this);        saveas = new jmenuitem("save as");       saveas.setaccelerator(keystroke.getkeystroke(keyevent.vk_a,                                                    actionevent.ctrl_mask));       filemenu.add(saveas);       // add action listener "save as" option       saveas.addactionlistener(this);        reset = new jmenuitem("reset");       reset.setaccelerator(keystroke.getkeystroke(keyevent.vk_r,                                                   actionevent.ctrl_mask));       filemenu.add(reset);       // add action listener "reset" option       reset.addactionlistener(this);        quit = new jmenuitem("quit");       quit.setaccelerator(keystroke.getkeystroke(keyevent.vk_q,                                                  actionevent.ctrl_mask));       filemenu.add(quit);       // add action listener "quit" option       quit.addactionlistener(this);      }      public void actionperformed(actionevent e) {       if(e.getsource().equals(quit)) {         closeprompt();         //main.win.dispose();       }       else if(e.getsource().equals(reset)) {         int n = joptionpane.showconfirmdialog(main.win,                                                "any player values will" +                                               " lost. proceed?",                                               "warning!", 2);         if(n == joptionpane.ok_option) {           main.board.reset();            main.view.repaint();         }       }       else if(e.getsource().equals(saveas)) {         saveas();       }       else if(e.getsource().equals(save)) {         if(filesaved == null) {           saveas();         }         else {           try {             board.writetostream(new fileoutputstream(filesaved)); //            main.board.setdirty(false);           } catch (exception ex) {             joptionpane.showmessagedialog(main.win, "error saving file.");           }         }       }        else if(e.getsource().equals(open)) {          int returnval = choose.showopendialog(main.win);         if(returnval == jfilechooser.approve_option) {           boolean error = false;           file openfile = choose.getselectedfile();            try {             fileinputstream fin = new fileinputstream(openfile);             objectinputstream ois = new objectinputstream(fin);             opener = ois.readobject();           } catch (exception ex) {             joptionpane.showmessagedialog(main.win, "error opening file.");             error = true;           }            if(opener != null && opener instanceof sudokubase){             main.west.remove(main.symbols);             main.east.remove(main.view);             //add in state information new board             main.south.remove(main.rowcolregstates);              main.view =  new sudokuview((sudokubase) opener);             main.symbols = new setsymbols(main.view);             //add in state information new board             main.rowcolregstates = new showstates(main.view);              main.west.add(main.symbols);             main.east.add(main.view);             //add in state information new board             main.south.add(main.rowcolregstates);              main.win.requestfocus();              filesaved = openfile; //            main.board.setdirty(false);           } else {             if(error) {               joptionpane.showmessagedialog(main.win, " incorrect file type!");             }           }         }         // else: user cancelled       }       else if(e.getsource().equals(newgame)) {         setenabled(false);         // create dialog box prompting new board information         createnewwin = new dialog1(main, "create new board", true);         // make visible         createnewwin.setvisible(true);          filesaved = null;       }     }      // method prompts user choose file save to,     // , saves file.     private int saveas() {       boolean saveerror;       int rtn = choose.showsavedialog(main.win);        if(rtn == jfilechooser.approve_option) {         saveerror = false;         file filesaveas = choose.getselectedfile();         try {           board.writetostream(new fileoutputstream(filesaveas));         } catch (exception e) {           joptionpane.showmessagedialog(main.win, "error saving file.");           saveerror = true;         }          if(!saveerror) {           filesaved = filesaveas; //          main.board.setdirty(false);         }       }        return rtn;      }      /**      * asks user if want save before closing if changes made.      */     private void closeprompt() {       if(true) {  //board.isdirty()) {         int n = joptionpane.showconfirmdialog(main.win, "save game?");         if(n == joptionpane.yes_option) {           int saved = saveas();           if(saved != jfilechooser.cancel_option){             main.win.dispose();           }         }         else if(n == joptionpane.no_option) {           main.win.dispose();         }       }       else         main.win.dispose();     }    } 

here first portion of sudokumain class (i accidentally put "sudokumain2" instead of "sudokumain" in menuattop, ignore 2):

// allow short name access following classes import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.io.*;  public class sudokumain extends jcomponent {    /**    * application method.    *     * @param args command-line arguments.    */   public static void main(string[] args) {     new sudokumain();   }    // field refers sudokubase class access information   // of board.   private sudokubase board;   // field refers sudokuview object access information   // , provide output   private sudokuview view;    // window components contained in   private jframe win;   // center jpanel object in window   private jpanel center;   // left jpanel object in window   private jpanel west;   // right jpanel object in window   private jpanel east;   // bottom jpanel object in window   private jpanel south;    // jpanel object hold graphic "buttons"   private jpanel symbols;   // jpanel object show "states" of sudoku board   private jpanel rowcolregstates;    // first set-up window (cannot changed once   // instantiated)   private final dialog1 setwin1;    /**    * constructs sudokumain object.    */   public sudokumain() {      // start game     board = makeboard();     view = new sudokuview(board);      win = new jframe("sudoku game");     center = new jpanel();     west = new jpanel();     east = new jpanel();     south = new jpanel();      // graphic "buttons" current sudoku board     symbols = new setsymbols(view);     // "states" of current sudoku board     rowcolregstates = new showstates(view);      // first set-up window     setwin1 = new dialog1(this, "new game", true);      // create menu bar     final menuattop menubar = new menuattop(this);     win.setjmenubar(menubar);      // display game mode     jlabel mode = new jlabel("normal play mode");     mode.sethorizontalalignment(jlabel.center);     font modefont = new font("arial", font.bold, 14);     mode.setfont(modefont);  //    // set selected cell @ (0, 0) //    view.setselected(0, 0);      // add window focus listener     win.addwindowfocuslistener(new windowfocuslistener() {        public void windowgainedfocus(windowevent e) {         win.pack();         // set selected cell @ (0, 0)         view.setselected(0, 0);       }        public void windowlostfocus(windowevent e) {       }     });      // add window listener     win.addwindowlistener(new windowadapter() {       public void windowclosing(windowevent e) {         menubar.closeprompt();       }     });      win.setlayout(new borderlayout());     west.setlayout(new borderlayout());     east.setlayout(new borderlayout());     center.setlayout(new flowlayout());      west.add(symbols);     east.add(view, borderlayout.center);     south.add(rowcolregstates);     center.add(west);     center.add(east);      win.add(south, borderlayout.north);     win.add(center, borderlayout.center);     win.add(mode, borderlayout.south);      win.setdefaultcloseoperation(jframe.dispose_on_close);     win.pack();     win.setvisible(true);    }    // inner class constructs graphic "buttons" set desired   // cells of board   class sudokucontrolbutton extends jpanel {      // row of selected cell     private int selrow;     // column of selected cell     private int selcol;      // value corresponds desired symbol     private int value;      /**      * constructs sudokucontrolbutton object; graphic "button"      * control board.      *       * @param view sudokuview object controlled.      * @param v value corresponds desired symbol.      */     public sudokucontrolbutton(final sudokuview view, int v) {       // set characteristics of graphic "button"       setpreferredsize(new dimension(50, 50));       setbackground(color.light_gray);        value = v;        addmouselistener(new mouselistener() {          /**          * method selects "button" , puts in focus when mouse          * clicked on it.          *           * @param event captures information on mouse button being          *              clicked (pressed , released) on component.          */         public void mouseclicked(mouseevent e) {           selrow = view.getselectedrow();           selcol = view.getselectedcolumn();            if(!board.isgiven(selrow, selcol)) {             board.setvalue(selrow, selcol, value);             view.new sudokucell(selrow, selcol, board);             // set "highlighted" color             setbackground(color.white);             view.repaint();           }           else {  // have system beep sound             gettoolkit().beep();           }            repaint();         }          /**          * method handles behavior when mouse enters graphic          * "button".          *           * @param event captures information on mouse button being          *              entered on component.          */          public void mouseentered(mouseevent e){           // set "highlighted" color           setbackground(color.white);            repaint();         }          /**          * method handles behavior when mouse exits graphic          * "button".          *           * @param event captures information on mouse button being          *              exited component.          */         public void mouseexited(mouseevent e){           // set default color           sudokucontrolbutton button = (sudokucontrolbutton) e.getsource();            setbackground(color.light_gray);            repaint();         }          /**          * method handles behavior when mouse pressed on          * graphic "button".          *           * @param event captures information on mouse button being          *              pressed on component.          */         public void mousepressed(mouseevent e){           // set "active" color           setbackground(color.yellow);            repaint();         }          /**          * method handles behavior when mouse released on          * graphic "button".          *           * @param e captures information on mouse button being          *              released on component.          */         public void mousereleased(mouseevent e){         }        });      }      /**      * method draws graphic "button" associated      * each numeric value, 0 12.      *       * @param g drawing mechanism.      */     public void paintcomponent(graphics g) {       super.paintcomponent(g);        switch(value) {         case 0:           drawsymbol(g, 0);           break;         case 1:           drawsymbol(g, 1);           break;         case 2:           drawsymbol(g, 2);           break;         case 3:           drawsymbol(g, 3);           break;         case 4:           drawsymbol(g, 4);           break;         case 5:           drawsymbol(g, 5);           break;         case 6:           drawsymbol(g, 6);           break;         case 7:           drawsymbol(g, 7);           break;         case 8:           drawsymbol(g, 8);           break;         case 9:           drawsymbol(g, 9);           break;         case 10:           drawsymbol(g, 10);           break;         case 11:           drawsymbol(g, 11);           break;         case 12:           drawsymbol(g, 12);           break;       }      }      /**      * method draws symbol corresponds       * specified value (0-12).      *       * @param g drawing mechanism.      * @param value specified value.      */     public void drawsymbol(graphics g, int value) {        if(value < 0 || value > 12) {         string msg = "value cannot less 0 or greater 12.";         throw new illegalargumentexception(msg);       }        // enable drawing "thick" lines       graphics2d g2 = (graphics2d) g;       g2.setstroke(new basicstroke(3));        switch(value) {         case 0:           // draw borders           g.drawrect(0, 0, 50, 50);           break;         case 1:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           break;         case 2:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           break;         case 3:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           break;         case 4:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           g2.drawline(20, 5, 20, 45);           break;         case 5:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           g2.drawline(20, 5, 20, 45);           g2.drawline(25, 5, 25, 45);           break;         case 6:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 45);           g2.drawline(10, 5, 10, 45);           g2.drawline(15, 5, 15, 45);           g2.drawline(20, 5, 20, 45);           g2.drawline(25, 5, 25, 45);           g2.drawline(30, 5, 30, 45);           break;         case 7:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 20);           g2.drawline(10, 5, 10, 20);           g2.drawline(15, 5, 15, 20);           g2.drawline(20, 5, 20, 20);           g2.drawline(25, 5, 25, 20);           g2.drawline(30, 5, 30, 20);           g2.drawline(5, 30, 5, 45);           break;         case 8:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 20);           g2.drawline(10, 5, 10, 20);           g2.drawline(15, 5, 15, 20);           g2.drawline(20, 5, 20, 20);           g2.drawline(25, 5, 25, 20);           g2.drawline(30, 5, 30, 20);           g2.drawline(5, 30, 5, 45);           g2.drawline(10, 30, 10, 45);           break;         case 9:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g2.drawline(5, 5, 5, 20);           g2.drawline(10, 5, 10, 20);           g2.drawline(15, 5, 15, 20);           g2.drawline(20, 5, 20, 20);           g2.drawline(25, 5, 25, 20);           g2.drawline(30, 5, 30, 20);           g2.drawline(5, 30, 5, 45);           g2.drawline(10, 30, 10, 45);           g2.drawline(15, 30, 15, 45);           break;         case 10:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g.drawline(5, 5, 5, 20);           g.drawline(10, 5, 10, 20);           g.drawline(15, 5, 15, 20);           g.drawline(20, 5, 20, 20);           g.drawline(25, 5, 25, 20);           g.drawline(30, 5, 30, 20);           g.drawline(5, 30, 5, 45);           g.drawline(10, 30, 10, 45);           g.drawline(15, 30, 15, 45);           g.drawline(20, 30, 20, 45);           break;         case 11:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g.drawline(5, 5, 5, 20);           g.drawline(10, 5, 10, 20);           g.drawline(15, 5, 15, 20);           g.drawline(20, 5, 20, 20);           g.drawline(25, 5, 25, 20);           g.drawline(30, 5, 30, 20);           g.drawline(5, 30, 5, 45);           g.drawline(10, 30, 10, 45);           g.drawline(15, 30, 15, 45);           g.drawline(20, 30, 20, 45);           g.drawline(25, 30, 25, 45);           break;         case 12:           // draw borders           g.drawrect(0, 0, 50, 50);           // draw symbol           g.drawline(5, 5, 5, 20);           g.drawline(10, 5, 10, 20);           g.drawline(15, 5, 15, 20);           g.drawline(20, 5, 20, 20);           g.drawline(25, 5, 25, 20);           g.drawline(30, 5, 30, 20);           g.drawline(5, 30, 5, 45);           g.drawline(10, 30, 10, 45);           g.drawline(15, 30, 15, 45);           g.drawline(20, 30, 20, 45);           g.drawline(25, 30, 25, 45);           g.drawline(30, 30, 30, 45);           break;       }      }    }    // inner class provides jmenubar object @ top of   // board   class menuattop extends jmenubar implements actionlistener{      // sudokumain object dealing     private sudokumain main;      // "file" menu     private jmenu filemenu;     // "new game" option     private jmenuitem newgame;     // "open" option     private jmenuitem open;     // "save" option     private jmenuitem save;     // "save as" option     private jmenuitem saveas;     // "reset" option     private jmenuitem reset;     // "quit" option     private jmenuitem quit;      // ability choose files     private jfilechooser choose;      // saved file //    // compiler not allow "static" keyword     private file filesaved = null;      private object opener;      // jdialog object create dialog box prompt     // user new game information     private jdialog createnewwin;       /**      * constructs menuattop object.      *       * @param m sudokumain object referred to.      */     public menuattop(final sudokumain m) {        main = m;        opener = null;       choose = new jfilechooser();        // instantiate , bind reference       filemenu = new jmenu("file");       add(filemenu);        // instantiate , bind reference       newgame = new jmenuitem("new game");       newgame.setaccelerator(keystroke.getkeystroke(keyevent.vk_n,                                                     actionevent.ctrl_mask));       filemenu.add(newgame);       newgame.addactionlistener(this);        open = new jmenuitem("open");       open.setaccelerator(keystroke.getkeystroke(keyevent.vk_o,                                                  actionevent.ctrl_mask));       filemenu.add(open);       // add action listener "open" option       open.addactionlistener(this);        save = new jmenuitem("save");       save.setaccelerator(keystroke.getkeystroke(keyevent.vk_s,                                                  actionevent.ctrl_mask));       filemenu.add(save);       // add action listener "save" option       save.addactionlistener(this);        saveas = new jmenuitem("save as");       saveas.setaccelerator(keystroke.getkeystroke(keyevent.vk_a,                                                    actionevent.ctrl_mask));       filemenu.add(saveas);       // add action listener "save as" option       saveas.addactionlistener(this);        reset = new jmenuitem("reset");       reset.setaccelerator(keystroke.getkeystroke(keyevent.vk_r,                                                   actionevent.ctrl_mask));       filemenu.add(reset);       // add action listener "reset" option       reset.addactionlistener(this);        quit = new jmenuitem("quit");       quit.setaccelerator(keystroke.getkeystroke(keyevent.vk_q,                                                  actionevent.ctrl_mask));       filemenu.add(quit);       // add action listener "quit" option       quit.addactionlistener(this);      }      public void actionperformed(actionevent e) {        if(e.getsource().equals(newgame)) {          setenabled(false);         // create dialog box prompting new board information         createnewwin = new dialog1(main, "create new board", true);         // make visible         createnewwin.setvisible(true);          filesaved = null;        } else if(e.getsource().equals(open)) {          int returnval = choose.showopendialog(main.win);         if(returnval == jfilechooser.approve_option) {           boolean error = false;           file openfile = choose.getselectedfile();            try {             fileinputstream fin = new fileinputstream(openfile);             objectinputstream ois = new objectinputstream(fin);             opener = ois.readobject();           } catch (exception exc) {             joptionpane.showmessagedialog(main.win, "error opening file.");             error = true;           }            // "opener" reads , of type sudokubase           if(opener != null && opener instanceof sudokubase){             main.west.remove(main.symbols);             main.east.remove(main.view);             main.south.remove(main.rowcolregstates);              main.view =  new sudokuview((sudokubase) opener);             main.symbols = new setsymbols(main.view);             main.rowcolregstates = new showstates(main.view);              main.west.add(main.symbols);             main.east.add(main.view);             main.south.add(main.rowcolregstates);              main.win.requestfocus();              filesaved = openfile;           } else {             if(error) {               joptionpane.showmessagedialog(main.win, "incorrect file type.");             }           }         }        } else if(e.getsource().equals(save)) {          if(filesaved == null) {           saveasprompt();         } else {           try {             board.writetostream(new fileoutputstream(filesaved));           } catch (exception exc) {             joptionpane.showmessagedialog(main.win, "error saving file.");           }         }        } else if(e.getsource().equals(saveas)) {         saveasprompt();       } else if(e.getsource().equals(reset)) {          int n = joptionpane.showconfirmdialog(main.win,                                                "any player values will" +                                               " lost. proceed?",                                               "warning!", 2);         if(n == joptionpane.ok_option) {           main.board.reset();           main.view.repaint();         }        } else if(e.getsource().equals(quit)) {         closeprompt();       }      }      // method prompts user choose file save to,     // , saves file.     private int saveasprompt() {       boolean saveerror;       int rtn = choose.showsavedialog(main.win);        if(rtn == jfilechooser.approve_option) {         saveerror = false;         file filesaveas = choose.getselectedfile();         try {           board.writetostream(new fileoutputstream(filesaveas));         } catch (exception e) {           joptionpane.showmessagedialog(main.win, "error saving file.");           saveerror = true;         }          if(!saveerror) {           filesaved = filesaveas;         }       }        return rtn;      }      // method prompts user whether want save before     // closing, if changes occurred.     private void closeprompt() {       if(true) {         int n = joptionpane.showconfirmdialog(main.win, "save game?");         if(n == joptionpane.yes_option) {           int saved = saveasprompt();           if(saved != jfilechooser.cancel_option){             main.win.dispose();           }         } else if(n == joptionpane.no_option) {           main.win.dispose();         }       }       else { // no changes made         main.win.dispose();       }     }    } 

...


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -