Ich versuche, den Status meines Swing-Programms, in diesem Fall eines Minesweeper-Spiels, zu speichern und neu zu laden. Mein Code für das Board ist unten.
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 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 = 0)
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 + 1;
if (cell 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 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 MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
if (current_col = 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 MINE_CELL) {
field[cell] -= COVER_FOR_CELL;
if (field[cell] == EMPTY_CELL)
find_empty_cells(cell);
}
cell = j + 1;
if (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 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 MINE_CELL) {
rep = true;
if (field[(cRow * cols) + cCol] 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]
Ihre Antwort
0 antwortet
Hot-Tools-Tags
Heiße Fragen
So übergeben Sie ein Array an eine gespeicherte SQL Server-Prozedur
2026-01-31 22:24:05
Was tun bei MySQL-Problemen? Fehler wie mysqli_fetch_array(): Argument #1 muss vom Typ mysqli_result und dergleichen sein
2026-01-31 22:02:53
Konvertieren Sie einen JavaScript-String in Punktnotation in eine Objektreferenz
2026-01-31 21:22:07
MySQL-Fehler Nr. 2014 – Befehle nicht synchron; Sie können diesen Befehl jetzt nicht ausführen [geschlossen]
2026-01-31 21:01:19
Wie formatiere ich ein Datum in JavaScript?
2026-01-31 20:43:18
Beliebtes Werkzeug
Sammlung der Laufzeitbibliothek vc9-vc14 (32+64 Bit) (Link unten)
Laden Sie die Sammlung der Laufzeitbibliotheken herunter, die für die Installation von phpStudy erforderlich sind
VC9 32-Bit
VC9 32-Bit-Laufzeitbibliothek für die integrierte Installationsumgebung von phpstudy
Vollversion der PHP-Programmierer-Toolbox
Programmer Toolbox v1.0 PHP Integrierte Umgebung
VC11 32-Bit
VC11 32-Bit-Laufzeitbibliothek für die integrierte Installationsumgebung von phpstudy
SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen
Heißer Artikel
Was soll ich tun, wenn ich nicht immer den Platz erhalte? Ouyitun Bibao Strategie zur Rotation und Neuausrichtung mehrerer Währungen
2026-04-04
By DDD
Wie spielt man Optionshandel? Detaillierte Analyse vom Anfänger bis zum Experten in Binance-Optionen
2026-03-30
By DDD
Maximale Speichergrenze des PHP-Laufzeitskripts zum Abrufen und Überwachen (Bytes)
2026-04-01
By DDD
14 versteckte iOS 26-Funktionen, die Sie wahrscheinlich verpasst haben
2026-04-01
By DDD






