ACM グラフィックス ライブラリを使用して Atari Breakout ゲームのクローンを作成し、ハイスコア インターフェイスと機能の追加を完了しました。プレーヤーの名前とスコアが GUI ウィンドウに表示され (正常に表示されます)、.dat
バイナリ ファイルにも書き込まれます。
ただし、コードが既存のファイルをロードしようとすると、次のエラーが発生します。
writing aborted; java.io.NotSerializableException: acm.graphics.GCanvasListener
このエラーをオンラインで調査したところ、クラスを編集して実装するSerializable
ことで解決できるようです。ただし、このエラーをスローするクラスは私自身のものではなく、サードパーティの ACM グラフィックス ライブラリに属するクラスです。これを解決するにはどうすればよいですか?
シリアル化しようとしているデータは名前とスコアにすぎないため、そもそもこのエラーが発生する理由もわかりません。オブジェクトのキャンバスなどをシリアル化しようとしているわけではありません。
メインクラス (ブレイクアウトと呼ばれる)
public class Breakout extends GraphicsProgram {
... // game variables
public void run() {
... // this if clause runs when game ends
if (brickCounter > 0) {
removeAll(); // clears screen
printGameOver(); // displays game over message
HighscoreManager hm = new HighscoreManager();
String name = getHighScoreName();
hm.addScore(name, score);
hm.displayHighscores();
}
}
... // game functionality methods
private String getHighScoreName(){
IODialog dialog = new IODialog();
String name = dialog.readLine("Enter your name: ");
return name;
}
スコアクラス
private class Score implements Serializable {
private int score;
private String name;
public Score(String name, int score) {
this.score = score;
this.name = name;
}
public int getScore() { return score; }
public String getName() { return name; }
}
ScoreComparator クラス
private class ScoreComparator implements Comparator<Score> {
public int compare(Score score1, Score score2) {
int sc1 = score1.getScore();
int sc2 = score2.getScore();
if (sc1 > sc2) {
return -1;
} else if (sc1 < sc2) {
return 1;
} else {
return 0;
}
}
}
HighscoreManager クラス
private class HighscoreManager {
private ArrayList<Score> scores;
private static final String HIGHSCORE_FILE = ".//bin//scores.dat";
ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;
public HighscoreManager() {
scores = new ArrayList<Score>(10);
}
public ArrayList<Score> getScores() {
loadScoreFile();
sort();
return scores;
}
private void sort() {
ScoreComparator comparator = new ScoreComparator();
Collections.sort(scores, comparator);
}
public void addScore(String name, int score) {
loadScoreFile();
scores.add(new Score(name, score));
updateScoreFile();
}
public void loadScoreFile() {
try {
inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));
scores = (ArrayList<Score>) inputStream.readObject();
}
catch (FileNotFoundException e) {
System.out.println("[Load] File Not Found Error: " + e.getMessage());
}
catch (IOException e) {
System.out.println("[Load] Input/Output Error: " + e.getMessage());
}
catch (ClassNotFoundException e) {
throw new RuntimeException("[Load] Class Not Found Error: " + e.getMessage());
}
finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
System.out.println("[Load] Input/Output Error: " + e.getMessage());
}
}
}
public void updateScoreFile() {
try {
outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE));
outputStream.writeObject(scores);
}
catch (FileNotFoundException e) {
System.out.println("[Update] File Not Found Error: " + e.getMessage());
}
catch (IOException e) {
System.out.println("[Update] Input/Output Error: " + e.getMessage());
}
finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
System.out.println("[Update] Input/Output Error: " + e.getMessage());
}
}
}
public void displayHighscores() {
int max = 10;
ArrayList<Score> scores;
scores = getScores();
int x = scores.size();
if (x > max) {
x = max;
}
removeAll(); // clears screen
int npos = 160;
int spos = 160;
for (int i = 0; i < x; i++) {
GLabel showName = new GLabel(scores.get(i).getName(), (getWidth() / 2.0) - 100, (getHeight() / 2.0) - npos);
showName.move(-showName.getWidth() / 2, -showName.getHeight());
showName.setColor(Color.WHITE);
add(showName);
npos -= 40;
}
for (int i = 0; i < x; i++) {
GLabel showScore = new GLabel(Integer.toString(scores.get(i).getScore()), (getWidth() / 2.0) + 100, (getHeight() / 2.0) - spos);
showScore.move(-showScore.getWidth() / 2, -showScore.getHeight());
showScore.setColor(Color.WHITE);
add(showScore);
spos -= 40;
}
}
アプリケーションを実行した後:
[Load] Input/Output Error: writing aborted; java.io.NotSerializableException: acm.graphics.GCanvasListener
[Update] Input/Output Error: acm.graphics.GCanvasListener
[Load] Input/Output Error: writing aborted; java.io.NotSerializableException: acm.graphics.GCanvasListener