我做了一个leveleditor,我可以将它保存为xml并加载。当我从xml加载并将统计信息设置到画布的网格中时,它会在2secounds之后立即显示,它会取消显示并显示旧的网格,但是为什么呢?这是我的网格类:
private int gridSizeX, gridSizeY;
private Color color;
public boolean isFinish = false;
private Tile[][] tile;
private int pushID;
public Grid(int gridSizeX, int gridSizeY) {
this.gridSizeX = gridSizeX;
this.gridSizeY = gridSizeY;
tile = new Tile[gridSizeX][gridSizeY];
for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
tile[x][y] = new Tile(x * 32, y * 32);
tile[x][y].setTileID(pushID);
pushID++;
}
}
}
public void Update() {
for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
tile[x][y].Update();
}
}
}
@Override
public void paintComponent(Graphics g) {
for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
tile[x][y].paintComponent(g);
isFinish = true;
}
}
super.paintComponent(g);
}
public Tile getTileAt(int x, int y) {
try {
return tile[x][y];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("NULL");
}
return null;
}
public void setTileAt(int x, int y, Color color1) {
tile[x][y].setColor(color1);
}
public void Load(String filePath) {
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File(filePath);
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List<Element> list = null;
for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
list = rootNode.getChildren();
}
}
Element node = null;
for (int i = 0; i < list.size(); i++) {
node = (Element) list.get(i);
System.out.println(" XPos : " + node.getAttributeValue("XPos"));
System.out.println(" YPos : " + node.getAttributeValue("YPos"));
System.out.println(" Image : " + node.getAttributeValue("Image"));
System.out.println(" ID : " + node.getAttributeValue("ID"));
}
for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
tile[x][y].setTileID(Integer.parseInt(list.get(tile[x][y].getTileID()).getAttributeValue("ID")));
tile[x][y].setX(Integer.parseInt(list.get(tile[x][y].getTileID()).getAttributeValue("XPos")));
tile[x][y].setY(Integer.parseInt(list.get(tile[x][y].getTileID()).getAttributeValue("YPos")));
tile[x][y].setImagePath(list.get(tile[x][y].getTileID()).getAttributeValue("Image"));
System.out.println(tile[x][y].getTileID());
}
}
} catch (IOException io) {
System.out.println(io.getMessage());
} catch (JDOMException jdomex) {
System.out.println(jdomex.getMessage());
}
}
public void ClearGrid() {
for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
}
}
}
public void Save() {
try {
Element company = new Element("Tiles");
Document doc = new Document(company);
for (int x = 0; x < gridSizeX; x++) {
for (int y = 0; y < gridSizeY; y++) {
Element daten = new Element("Daten");
daten.setAttribute(new Attribute("ID", String.valueOf(tile[x][y].getTileID())));
daten.setAttribute(new Attribute("XPos", String.valueOf(tile[x][y].getX())));
daten.setAttribute(new Attribute("YPos", String.valueOf(tile[x][y].getY())));
daten.setAttribute(new Attribute("Image", tile[x][y].getImagePath()));
doc.getRootElement().addContent(daten);
}
}
XMLOutputter output = new XMLOutputter();
output.setFormat(Format.getPrettyFormat());
output.output(doc, new FileWriter("Test.xml"));
System.out.println("XML File was created successfully!");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!