Path: blob/master/SLICK_HOME/src/org/newdawn/slick/XMLPackedSheet.java
1456 views
package org.newdawn.slick;12import java.util.HashMap;34import javax.xml.parsers.DocumentBuilder;5import javax.xml.parsers.DocumentBuilderFactory;67import org.newdawn.slick.util.ResourceLoader;8import org.w3c.dom.Document;9import org.w3c.dom.Element;10import org.w3c.dom.NodeList;1112/**13* A sprite sheet based on an XML descriptor generated from the simple slick tool14*15* @author kevin16*/17public class XMLPackedSheet {18/** The full sheet image */19private Image image;20/** The sprites stored on the image */21private HashMap sprites = new HashMap();2223/**24* Create a new XML packed sheet from the XML output by the slick tool25*26* @param imageRef The reference to the image27* @param xmlRef The reference to the XML28* @throws SlickException Indicates a failure to parse the XML or read the image29*/30public XMLPackedSheet(String imageRef, String xmlRef) throws SlickException31{32image = new Image(imageRef, false, Image.FILTER_NEAREST);3334try {35DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();36Document doc = builder.parse(ResourceLoader.getResourceAsStream(xmlRef));3738NodeList list = doc.getElementsByTagName("sprite");39for (int i=0;i<list.getLength();i++) {40Element element = (Element) list.item(i);4142String name = element.getAttribute("name");43int x = Integer.parseInt(element.getAttribute("x"));44int y = Integer.parseInt(element.getAttribute("y"));45int width = Integer.parseInt(element.getAttribute("width"));46int height = Integer.parseInt(element.getAttribute("height"));4748sprites.put(name, image.getSubImage(x,y,width,height));49}50} catch (Exception e) {51throw new SlickException("Failed to parse sprite sheet XML", e);52}53}5455/**56* Get a sprite by it's given name57*58* @param name The name of the sprite to retrieve59* @return The sprite from the sheet or null if the name isn't used in this sheet60*/61public Image getSprite(String name) {62return (Image) sprites.get(name);63}64}656667