gpt4 book ai didi

java - ObjectOutputStream 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:21 26 4
gpt4 key购买 nike

我正在尝试编写一款地下城大师游戏,英雄将穿过不同的房间并与怪物战斗并获取元素。我想每次英雄达到新级别时都保存它,但即使我已经在英雄及其使用的所有内容上实现了可序列化,我还是收到了IOException。一些帮助将非常感激。谢谢。

具有保存方法的面板:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* The panel where the game happens.
*/
public class Panel extends JPanel implements Runnable, KeyListener {
private Thread t;
private Hero hero;
private File f;
...
public Panel(String n, int g) {
f = new File("save.dat");
...
}
public void save() {
try {
JOptionPane.showMessageDialog(null, "Saving...");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(hero);
out.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error saving file.");
}
}

英雄类:

import java.awt.Point;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
/**
* Our awesome hero.
*/
public class Hero extends Character implements Serializable {
/** Array list of items of hero */
private ArrayList<Item> items;
/** Character's location */
private Point location;
private int dx, dy, tempX, tempY;
/**
* Constructor
*
* @param n
* Hero's name.
* @param q
* Hero's catch phrase.
* @param start
* Hero's starting point.
*/
public Hero(String n, String q, Point start) {
super(n, q, 15, 1, 0);
location = start;
items = new ArrayList<Item>();
tempX = (int) location.getX();
tempY = (int) location.getY();
}
/**
* Drawing the hero.
*
* @param g The graphics
*/
public void drawHero(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect((int) location.getX(), (int) location.getY(), 50, 50);
move();
}
/**
* Moving the hero.
*/
public void move() {
setX(getX() + dx);
setY(getY() + dy);
}
/**
* Stopping the hero.
*/
public void stop() {
dx = 0;
dy = 0;
}
/**
* Attacking the enemy.
*
* @param C The enemy that will be attacked.
*/
public void attack(Character C) {
int h = (int) (Math.random() * 3) + 1;
h += getLevel();
C.takeDamage(h);
}
/**
* Get the items.
*
* @return The item arraylist.
*/
public ArrayList<Item> getItems() {
return items;
}
/**
* Picking up the item and check if
* inventory is full.
*/
public boolean pickUpItem(Item i) {
if (items.size() == 5)
return false;
else {
items.add(i);
return true;
}
}
/**
* Check what room is the hero in.
*/
public char checkRoom(Level l) {
int x = (int) location.getX();
int y = (int) location.getY();
if ((x < 400 && y < 400 && x >= 0 && y >= 0)) {
return l.getRoom(location);
} else
return 'n';
}
/**
* Remove item by its name.
*
* @param i The item to be removed.
*/
public void removeItem(Item i) {
int item = items.indexOf(i);
items.remove(item);
}

/**
* Remove item by its index.
*
* @param index The index of the item to be removed.
*/
public void removeItem(int index) {
index -= 1;
Item i = items.get(index);
int gold = i.getValue();
collectGold(gold);
items.remove(index);
}
/**
* Getting hero's location.
*
* @return The hero's location.
*/
public Point getLocation() {
return location;
}
/**
* Setting the hero's location.
*
* @param p The location that will be set to.
*/
public void setLocation(Point p) {
int x = (int) p.getX();
int y = (int) p.getY();
location.setLocation(x, y);
}
/**
* Checking to see if hero is out of bounds.
*/
public void checkBounds() {
if ( getX() <= 0 || getX()+50 >= 400 || getY() <= 0 || getY()+50 >= 400 ) {
dx = 0;
dy = 0;
}
tempX = getX();
tempY = getY();
}
/**
* Stopping the hero.
*/
public void staph() {
dx = 0;
dy = 0;
setX(tempX);
setY(tempY);
}
/**
* Going north.
*/
public void goNorth() {
if ( getY() > 0 ) {
dx = 0;
dy = -2;
}
}
/**
* Going south.
*/
public void goSouth() {
if ( getY()+50 < 400 ) {
dx = 0;
dy = 2;
}
}
/**
* Going east.
*/
public void goEast() {
if ( getX()+50 < 400 ) {
dx = 2;
dy = 0;
}
}
/**
* Going west.
*/
public void goWest() {
if ( getX() > 0 ) {
dx = -2;
dy = 0;
}
}
/**
* Running north.
*/
public void runNorth() {
setY(getY() - 100);
}
/**
* Running north.
*/
public void runSouth() {
setY(getY() + 100);
}
/**
* Running north.
*/
public void runEast() {
setX(getX() + 100);
}
/**
* Running north.
*/
public void runWest() {
setX(getX() - 100);
}
/**
* Getting x location of hero.
*
* @return Hero's x location.
*/
public int getX() {
return (int) location.getX();
}
/**
* Getting y location of hero.
*
* @return Hero's y location.
*/
public int getY() {
return (int) location.getY();
}
/**
* Setting x location of hero.
*/
public void setX(double x) {
location.setLocation(x, getY());
}
/**
* Setting y location of hero.
*/
public void setY(double y) {
location.setLocation(getX(), y);
}
/**
* Check if hero is active.
*/
public boolean isActive() {
return getHP() > 0;
}
}

项目类别:

import java.io.Serializable;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
/**
* The item that the hero will have.
*/
public class Item implements Serializable {
/** Item's level */
private String name;
/** Item's gold value */
private int goldValue;
private BufferedImage img;
/**
* Constructor
*
* @param n
* Item's name.
* @param v
* Item's gold value.
*/
public Item(String n, int v, BufferedImage i) {
name = n;
goldValue = v;
img = i;
}

/**
* Get the name of the item.
*
* @return the name of the item.
*/
public String getName() {
return name;
}

/**
* Get the value of the item.
*
* @return The gold value of the item.
*/
public int getValue() {
return goldValue;
}

public BufferedImage getImage() {
return img;
}
}

最佳答案

您需要将变量img更改为transient

private transient BufferedImage img;

这将防止异常。尽管您不会将图像保存为 transient 方式,但不要写入此字段。我想说,您需要将 BufferedImage 的字节序列化为 bytes[] 或类似的可验证类型。

transient 关键字是 here explained 。它说

4) Transient variable in java is not persisted or saved when an object gets serialized.

了解更多:http://javarevisited.blogspot.com/2011/09/transient-keyword-variable-in-java.html#ixzz3ZeGZZQHd

关于java - ObjectOutputStream 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com