从Xml加载到Canvas并消失

时间:2015-04-28 07:13:11

标签: java xml canvas

我制作了一个Leveleditor我可以将它保存到xml并加载。当我从xml加载并将统计数据设置为它在2秒后显示的画布的网格时,它会消失并显示旧网格,但为什么呢? 这是我的网格类:

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();
    }

}

}

0 个答案:

没有答案