mammothkiosk.Walkway.java Source code

Java tutorial

Introduction

Here is the source code for mammothkiosk.Walkway.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mammothkiosk;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

/**
 * Class defining the Walkway data structure.
 * @author Jaysen Spurlock
 * @author Paul Blasi
 * @author Joe Manke
 */
public class Walkway {

    public Point.Float min;
    public Point.Float max;
    private List<PolyLine> polylines;

    /**
     * Default constructor.
     */
    public Walkway() {
        // Retrieve info for drawing bone from respective bone-shape file
        SAXBuilder saxBuild = new SAXBuilder();
        Document doc;

        try {
            doc = saxBuild.build("../bonexml/walkway.xml");

            Element rootElement = doc.getRootElement();
            parseFile(rootElement);
        } catch (Exception ex) {
            System.out.println("Exception in Bone Constructor: " + ex.getMessage());
        }
    }

    /**
     * Parses walkway.xml to grab values.
     * @param root The root element to parse
     */
    private void parseFile(Element root) {
        // Grab minimum X/Y values
        String parseMe = root.getChild("xymin").getValue();
        int space = parseMe.substring(1).indexOf(" ");
        min = new Point2D.Float(Float.parseFloat(parseMe.substring(0, space)),
                Float.parseFloat(parseMe.substring(space + 1)));

        // Grab maximum X/Y values
        parseMe = root.getChild("xymax").getValue();
        space = parseMe.substring(1).indexOf(" ");
        max = new Point2D.Float(Float.parseFloat(parseMe.substring(0, space)),
                Float.parseFloat(parseMe.substring(space + 1)));

        // Parse the shape
        Element shape = root.getChild("shape");

        polylines = new ArrayList<>();

        for (Element curElement : shape.getChildren("polyline")) {
            PolyLine polyline = new PolyLine();

            for (Element coord : curElement.getChildren("xy")) {
                space = coord.getValue().substring(1).indexOf(" ");

                polyline.addX(Float.parseFloat(coord.getValue().substring(0, space)));
                polyline.addY(Float.parseFloat(coord.getValue().substring(space + 1)));
            }

            polylines.add(polyline);
        }
    }

    /**
     * Getter for the polylines member.
     * 
     * @return The shape of the walkway, defined as a list of polylines
     */
    public List<PolyLine> getShape() {
        return polylines;
    }
}