Java 如何保存程序的状态并加载它?

Java 如何保存程序的状态并加载它?,java,swing,serialization,imageicon,Java,Swing,Serialization,Imageicon,我正在尝试保存并重新加载Swing程序的状态,在本例中,这是一个扫雷游戏。我的董事会代码如下 package mines; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Random; import javax.swing.BorderFactory; import

我正在尝试保存并重新加载Swing程序的状态,在本例中,这是一个扫雷游戏。我的董事会代码如下

package mines;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Board extends JPanel {

    public static void main (String[] args)  {}
    private final int NUM_IMAGES = 13;
    private final int CELL_SIZE = 15;

    private final int COVER_FOR_CELL = 10;
    private final int MARK_FOR_CELL = 10;
    private final int EMPTY_CELL = 0;
    private final int MINE_CELL = 9;
    private final int COVERED_MINE_CELL = MINE_CELL + COVER_FOR_CELL;
    private final int MARKED_MINE_CELL = COVERED_MINE_CELL + MARK_FOR_CELL;

    private final int DRAW_MINE = 9;
    private final int DRAW_COVER = 10;
    private final int DRAW_MARK = 11;
    private final int DRAW_WRONG_MARK = 12;

    private int[] field;
    private boolean inGame;
    private int mines_left;
    private Image[] img;
    private int mines = 40;
    private int rows = 16;
    private int cols = 16;
    private int all_cells;
    private JLabel statusbar;

    public Board(JLabel statusbar) {

        this.statusbar = statusbar;

        img = new Image[NUM_IMAGES];

        for (int i = 0; i < NUM_IMAGES; i++) {
            img[i] =
                (new ImageIcon(this.getClass().getResource((i)
                    + ".png"))).getImage();
        }

        setDoubleBuffered(true);

        addMouseListener(new MinesAdapter());

        newGame();
    }


    public void newGame() {

        Random random;
        int current_col;

        int i = 0;
        int position = 0;
        int cell = 0;

        random = new Random();
        inGame = true;
    mines_left = mines;

    all_cells = rows * cols;
    field = new int[all_cells];

    for (i = 0; i < all_cells; i++)
        field[i] = COVER_FOR_CELL;

    statusbar.setText(Integer.toString(mines_left));


    i = 0;
    while (i < mines) {

        position = (int) (all_cells * random.nextDouble());

        if ((position < all_cells) &&
            (field[position] != COVERED_MINE_CELL)) {


            current_col = position % cols;
            field[position] = COVERED_MINE_CELL;
            i++;

            if (current_col > 0) { 
                cell = position - 1 - cols;
                if (cell >= 0)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;
                cell = position - 1;
                if (cell >= 0)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;

                cell = position + cols - 1;
                if (cell < all_cells)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;
            }

            cell = position - cols;
            if (cell >= 0)
                if (field[cell] != COVERED_MINE_CELL)
                    field[cell] += 1;
            cell = position + cols;
            if (cell < all_cells)
                if (field[cell] != COVERED_MINE_CELL)
                    field[cell] += 1;

            if (current_col < (cols - 1)) {
                cell = position - cols + 1;
                if (cell >= 0)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;
                cell = position + cols + 1;
                if (cell < all_cells)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;
                cell = position + 1;
                if (cell < all_cells)
                    if (field[cell] != COVERED_MINE_CELL)
                        field[cell] += 1;
            }
        }
    }
}


public void find_empty_cells(int j) {

    int current_col = j % cols;
    int cell;

    if (current_col > 0) { 
        cell = j - cols - 1;
        if (cell >= 0)
            if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
            }

        cell = j - 1;
        if (cell >= 0)
            if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
            }

        cell = j + cols - 1;
        if (cell < all_cells)
            if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
            }
    }

      cell = j - cols;
    if (cell >= 0)
        if (field[cell] > MINE_CELL) {
            field[cell] -= COVER_FOR_CELL;
            if (field[cell] == EMPTY_CELL)
                find_empty_cells(cell);
        }

       cell = j + cols;
       if (cell < all_cells)
        if (field[cell] > MINE_CELL) {
            field[cell] -= COVER_FOR_CELL;
            if (field[cell] == EMPTY_CELL)
                find_empty_cells(cell);
           }

          if (current_col < (cols - 1)) {
           cell = j - cols + 1;
             if (cell >= 0)
               if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
            }

           cell = j + cols + 1;
            if (cell < all_cells)
               if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
            }

          cell = j + 1;
           if (cell < all_cells)
            if (field[cell] > MINE_CELL) {
                field[cell] -= COVER_FOR_CELL;
                if (field[cell] == EMPTY_CELL)
                    find_empty_cells(cell);
               }
        }

       }

      public void paint(Graphics g) {

       int cell = 0;
        int uncover = 0;


        for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {

            cell = field[(i * cols) + j];

            if (inGame && cell == MINE_CELL)
                inGame = false;

            if (!inGame) {
                if (cell == COVERED_MINE_CELL) {
                    cell = DRAW_MINE;
                } else if (cell == MARKED_MINE_CELL) {
                    cell = DRAW_MARK;
                } else if (cell > COVERED_MINE_CELL) {
                    cell = DRAW_WRONG_MARK;
                } else if (cell > MINE_CELL) {
                    cell = DRAW_COVER;
                }


            } else {
                if (cell > COVERED_MINE_CELL)
                    cell = DRAW_MARK;
                else if (cell > MINE_CELL) {
                    cell = DRAW_COVER;
                    uncover++;
                }
            }

            g.drawImage(img[cell], (j * CELL_SIZE),
                (i * CELL_SIZE), this);
        }
       }


      if (uncover == 0 && inGame) {
        inGame = false;
        statusbar.setText("Game won");
      } else if (!inGame)
        statusbar.setText("Game lost");
       }




     class MinesAdapter extends MouseAdapter {
       public void mousePressed(MouseEvent e) {

        int x = e.getX();
        int y = e.getY();

        int cCol = x / CELL_SIZE;
        int cRow = y / CELL_SIZE;

        boolean rep = false;


          if (!inGame) {
            newGame();
            repaint();
          }


         if ((x < cols * CELL_SIZE) && (y < rows * CELL_SIZE)) {

            if (e.getButton() == MouseEvent.BUTTON3) {

                if (field[(cRow * cols) + cCol] > MINE_CELL) {
                    rep = true;

                    if (field[(cRow * cols) + cCol] <= COVERED_MINE_CELL) {
                        if (mines_left > 0) {
                            field[(cRow * cols) + cCol] += MARK_FOR_CELL;
                            mines_left--;
                            statusbar.setText(Integer.toString(mines_left));
                        } else
                            statusbar.setText("No marks left");
                    } else {

                        field[(cRow * cols) + cCol] -= MARK_FOR_CELL;
                        mines_left++;
                        statusbar.setText(Integer.toString(mines_left));
                    }
                }

            } else {

                if (field[(cRow * cols) + cCol] > COVERED_MINE_CELL) {
                    return;
                }

                if ((field[(cRow * cols) + cCol] > MINE_CELL) &&
                    (field[(cRow * cols) + cCol] < MARKED_MINE_CELL)) {

                    field[(cRow * cols) + cCol] -= COVER_FOR_CELL;
                    rep = true;

                    if (field[(cRow * cols) + cCol] == MINE_CELL)
                        inGame = false;
                    if (field[(cRow * cols) + cCol] == EMPTY_CELL)
                        find_empty_cells((cRow * cols) + cCol);
                }
            }

            if (rep)
                repaint();

            }
        }
    }
}
包装矿山;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.util.Random;
导入javax.swing.BorderFactory;
导入javax.swing.ImageIcon;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共课程委员会扩展JPanel{
公共静态void main(字符串[]args){}
私有最终int NUM_图像=13;
专用最终整数单元格大小=15;
单元=10的专用最终整数保险;
单元的专用最终整数标记=10;
私有最终整型空_单元=0;
专用最终int矿山单元=9;
私有最终整数覆盖的\u MINE\u CELL=覆盖的\u CELL+覆盖的\u CELL;
专用最终整数标记的\u MINE\u单元格=覆盖的\u MINE\u单元格+标记的\u单元格;
私人最终int DRAW_MINE=9;
私人最终int DRAW_COVER=10;
私人最终整数抽签分数=11;
私人最终整数抽签错误分数=12;
私有int[]字段;
私有布尔inGame;
私家车,左;
私有图像[]img;
私人采矿=40;
私有int行=16;
私有整数cols=16;
私有int所有_单元;
专用JLabel状态栏;
公共板(JLabel状态栏){
this.statusbar=状态栏;
img=新图像[NUM_图像];
对于(int i=0;i0){
单元=位置-1-列;
如果(单元格>=0)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
单元=位置-1;
如果(单元格>=0)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
单元=位置+cols-1;
if(单元<所有单元)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
}
单元=位置-cols;
如果(单元格>=0)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
单元=位置+cols;
if(单元<所有单元)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
如果(当前列<(列-1)){
单元=位置-cols+1;
如果(单元格>=0)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
单元=位置+cols+1;
if(单元<所有单元)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
单元=位置+1;
if(单元<所有单元)
如果(字段[单元格]!=覆盖的\u矿山\u单元格)
字段[单元]+=1;
}
}
}
}
公共无效查找空单元格(int j){
int current_col=j%cols;
int细胞;
如果(当前列>0){
单元=j-cols-1;
如果(单元格>=0)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[单元格]==空单元格)
查找\u空\u单元格(单元格);
}
细胞=j-1;
如果(单元格>=0)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[单元格]==空单元格)
查找\u空\u单元格(单元格);
}
电池=j+cols-1;
if(单元<所有单元)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[单元格]==空单元格)
查找\u空\u单元格(单元格);
}
}
cell=j-cols;
如果(单元格>=0)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[单元格]==空单元格)
查找\u空\u单元格(单元格);
}
细胞=j+cols;
if(单元<所有单元)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[单元格]==空单元格)
查找\u空\u单元格(单元格);
}
如果(当前列<(列-1)){
单元=j-cols+1;
如果(单元格>=0)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[单元格]==空单元格)
查找\u空\u单元格(单元格);
}
电池=j+cols+1;
if(单元<所有单元)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[单元格]==空单元格)
查找\u空\u单元格(单元格);
}
细胞=j+1;
if(单元<所有单元)
if(字段[单元]>矿井单元){
字段[单元]-=覆盖单元的单元;
if(字段[cel