0

저장 /로드 메소드를 작성했습니다 (저장이 제대로 작동하는지 모르지만 'minesweepersavestate.ser'파일은 saveGame()을 호출 한 후 내 프로젝트 폴더에 나타납니다). 현재는 아무 것도하지 않는 것 같아서 부하를 작동 시키려고합니다.파일/오브젝트 입력 스트림을 사용하여 게임을로드하는 방법

여기 내 기능은 다음과 같습니다 내가 loadBoard() 메서드를 호출 할 때,이 상태에서 게임을 다시 제공하도록

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(); 
     } 

는 어떻게하면 저장된이 방법을 변경할 수 있습니까?

편집 :

게임 보드 클래스 :

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) { } 

} 

} 당신은 클래스의 모든 분야에서 알 수 Serializablethis tutorial로되어 있는지 확인해야

+0

당신은 어떤 예외를받을 수 있나요 ? 아니면 그냥 null입니까? – Frozendragon

+0

GameBoard 객체의 코드 개요를 제공 할 수 있습니까? 클래스 선언, 필드 및 메서드 서명 – sfedak

+0

수정 됨. 실제로 방금 저장이 작동하지 않는 것을 확인했습니다. 오류가 발생했습니다 : "AWT-EventQueue-0"예외 java.lang.NullPointerException \t GameBoard.click (GameBoard.java:434) – AW90

답변

0

. 특히 주목할 클래스에 대한 요구 사항이 Serializable 수 있습니다 : 이것은 당신이 당신의 GameBoard 클래스의 모든 필드에 대한 Javadoc과의 뒷조사를해야 할 것이다 의미

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.

. 예를 들어 JFrame에 대한 빠른 검색을 수행했으며 의 경우는 JFrame을 저장하는 데 약간의 nuance이있는 것으로 보입니다. 저장된 게임의 상태를 검색 할 때 Serialization을 사용하지 않고 처음부터 GUI를 다시 만드는 것이 좋습니다. This thread는 또한 당신을 위해 GUI를 복원 자바에 의존하는 것은 좋은 생각이 아니라고 동의 보이지만, 아마도 그것이 작동되도록하는 방법에 관한 제안을 제공합니다 당신이 그것을로드 할 때

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