2014-12-19 27 views
0

我已經編寫了保存/加載方法(如果保存工作但文件'minesweepersavestate.ser'在調用saveGame()後出現在我的項目文件夾中,則完全不確定)。我想嘗試讓負荷工作,因爲目前它似乎沒有做任何事情。如何使用文件/對象輸入流加載遊戲

這裏是我的功能:

public void saveGame(){ 

     GameBoard b = new GameBoard(); 
     try { 
      System.out.println("Creating File/Object output stream..."); 
      FileOutputStream fileOut = new FileOutputStream("minesweepersavestate.ser"); 
      ObjectOutputStream out = new ObjectOutputStream(fileOut); 

      System.out.println("Writing GameBoard Object..."); 
      out.writeObject(b); 

      System.out.println("Save Successful...\n"); 
      out.close(); 
      fileOut.close(); 

     } catch(FileNotFoundException e) { 
      System.out.println("no"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.out.println("no"); 
      e.printStackTrace(); 
     } 

    } 


    public void loadBoard() 
    { 
     GameBoard b = null; 
     try { 

      System.out.println("Creating File/Object input stream..."); 

      FileInputStream fileIn = new FileInputStream("minesweepersavestate.ser"); 
      ObjectInputStream in = new ObjectInputStream(fileIn); 

      System.out.println("Loading GameBoard Object..."); 
      b = (GameBoard)in.readObject(); 

      System.out.println("Load Successful...\n"); 
      in.close(); 
      fileIn.close(); 

     } catch (ClassNotFoundException e) { 
      System.out.println("Class not found\n"); 
      e.printStackTrace(); 
     } catch(FileNotFoundException e) { 
      System.out.println("File not found\n"); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

我怎樣才能改變這種方法,這樣,當我稱之爲loadBoard()方法,它帶來找回狀態的遊戲保存它?

編輯:

遊戲鍵盤類:

public class GameBoard extends JFrame implements Serializable 
{ 
//MENU ITEMS 
private JFrame frame; 
private JMenuBar menubar = new JMenuBar(); 
private JTable table; 

//FIELDS USED FOR GAMEPLAY 
private int x; 
private int y; 
private boolean mineTrue; 
private boolean triedTrue; 
private static int boardsize = 8; 
private int numberOfMines = 10; 
public static final int Empty = 0; 
public static final int Mine = -1; 
public static final int Flag = -2; 
public static final int FMine = -3; 
public static final int RevealedMine = -4; 
public static final int RevealedEmpty = -5; 
// SIZE OF BUTTONS 
private int gridsize = 45; 
// ARRAY FOR THE BUTTONS 
private JButton[][] buttons; 
private int[][] board; 
//VARIABLE USED FOR LABELS 
private static int noGamesPlayed = 0; 
private static int noGamesWon = 0; 
private int mine = 10; 
private int minesLeft = 10; 
private static int score = 1; 
private static String playername; 
// GAME STATUS 
private boolean gamegoing = true; 
// GAME LABELS 

private JLabel space = new JLabel(""); 
private JTextField nameEnter = new JTextField("Enter name here: "); 
private JButton saveName = new JButton("Play"); 
private JLabel namelabel = new JLabel("Player 1: "); 
private JLabel scorelabel = new JLabel("0 points "); 
private JLabel status = new JLabel("Game in Progress: "); 
private JLabel gamesPlayed = new JLabel("Games Played: " + noGamesPlayed); 
private JLabel gamesWon = new JLabel("Games Won : " + noGamesWon); 
private JLabel noMines = new JLabel("Number of Mines: " + minesLeft); 


/** 
* Constructor 
*/ 
public GameBoard() { } 

/** 
*Making the game Frame 
*/ 
private void makeFrame() { } 

// MAKING THE GAME MENU BAR 
public void makeMenuBar(){ 
} 

public void setx(int pmeter) { } 

public void sety(int pmeter) { } 

public int getx() { } 

public int gety() { } 

public void settried(boolean paramBoolean) { } 

public boolean gettried() { } 

public void setmine(boolean paramBoolean) { } 

public boolean getmine() { } 

//ASSIGN MINES TO RANDOM LOCATION 
public void assignmines() { } 

// *********************************GAME CONTROLS************ 
private void quit() { } 

//RESETS THE BOARD 
public void reset() { }  

public void newGame() { } 

public void biggerBoard() { } 

public void changeDifficulty() { } 

public void saveGame() { } 


public void loadBoard() { } 

// LOSING THE GAME ALERT 
public void lose() { } 

// WINNING THE GAME ALERT 
public void win() { } 

// UPDATING THE SCORE 
public void updatescore() { } 

public void gamesPlayed() { } 

public void UpdateName() { } 

public void updateGamesPlayed() { } 

public void updateGamesWon() { } 

//WHAT VALUES THE CHARACTER HAVE 
static char whichCharacter(int value) { } 

//This method shows how many mines are around the spot that was clicked 
static int minesAround(int[][] board, int row, int col) { } 

// This method takes in an x and y value and defines what should happen when the user clicks there. 
public void click(int rows, int cols) { } 

//ACTION WHEN USER CLICKS ON A BUTTON INNER CLASS 
private class MouseListener extends MouseAdapter { 
    private int x = 0; 
    private int y = 0; 

    public MouseListener(int row, int col) { } 

    public void mouseClicked(MouseEvent e) { } 

} 

}

+0

你什麼時候加載它,你得到任何異常?或者b只是空? – Frozendragon 2014-12-19 12:02:07

+0

您可以提供GameBoard對象的代碼大綱嗎?類聲明,字段和方法簽名? – sfedak 2014-12-19 12:12:57

+0

已編輯。其實我只是現在檢查保存不工作我只是得到了錯誤:線程「AWT-EventQueue-0」異常java.lang.NullPointerException \t在GameBoard.click(GameBoard.java:434) – AW90 2014-12-19 12:17:30

回答

0

你需要確保你的班上所有的領域都Serializablethis tutorial建議。特別值得注意的是一類的要求是Serializable

Notice that for a class to be serialized successfully, two conditions must be met:

The class must implement the java.io.Serializable interface.

All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

這意味着你必須做的Javadoc一些挖掘在您GameBoard類中的所有領域。例如,我在JFrame上做了一個快速搜索,看起來有點nuance,當涉及到 保存JFrame。當您檢索已保存遊戲的狀態時,最好從頭開始重新創建GUI,而不是依靠Serialization來爲您完成。 This thread似乎也同意,這是不依賴於Java來恢復GUI爲你一個好主意,但給出了一個建議,至於如何可能使其工作:

once deserialised, you will need to show the frame with frame.setVisible(true);