Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Graphical Output from SVG(XML) file

Status
Not open for further replies.

mfc2005

Programmer
Jul 25, 2005
8
GB
I am writing a program to read in an SVG (Scalable Vector Graphics) XML application file and simply display the graphical output of the tags using the attributes within the tags. I have read in the file using a file reader and parsed it using a DOM (see Below), iterating through to obtain the values required for the graphical objects. The problem I am having is that I am new to java and am unsure of an appropriate way to handle the attributes for each object obtained. I currently put them in a hashmap and then pass them to my graphics class but I am unsure when to use the paint method and how is best to pass the attributes i.e. should they be passed as attributes individually or as a shape object? How would you do this?

Example code:

import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.HashMap;
import javax.swing.*;

import java.awt.*;
import java.util.*;


public class TestDOM extends JComponent
{
private Container c;

public static void main( String [] args ) throws Exception
{

JFrame frame = new JFrame("Stereo");
Container c = frame.getContentPane();
c.setLayout(new BorderLayout());

//c.add(new TestDOM(), BorderLayout.CENTER);
frame.setSize(800, 600);
c.setBackground(Color.WHITE);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
TestDOM test = new TestDOM(c);
frame.setVisible(true);
}

public TestDOM(Container _c)throws Exception
{
c = _c;
createRoot();
}

public void createRoot()throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse( "Stereo01.svg" );

//Pick out data from SVG file
Element svg = document.getDocumentElement();
Attr svgWidth= svg.getAttributeNode("width");
Attr svgHeight = svg.getAttributeNode("height");
String widthVal = svgWidth.getValue();
String heightVal = svgHeight.getValue();
System.out.println("SVG Width: " +widthVal);
System.out.println("SVG Height: " +heightVal);
getElem(svg);

}

public void getElem(Element svg)
{
NodeList rectList = svg.getElementsByTagName("rect");

for(int i=0; i<rectList.getLength(); i++)
{
Node rect = rectList.item(i);
NamedNodeMap rectMap = rect.getAttributes();
HashMap rMap = new HashMap();

for(int iattr = 0; iattr < rectMap.getLength(); iattr++)
{
Node attr = rectMap.item(iattr);
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
//System.out.println("Attr: " + attrName);
//System.out.println("Value: " + attrValue);
rMap.put(attrName, attrValue);
//String x = (String)hMap.get("x");
//if (x != null)
//{
// System.out.println("x Attr" + x);
//}

}
String xRect = (String)rMap.get("x");
String yRect = (String)rMap.get("y");
String rectWidth = (String)rMap.get("width");
String rectHeight = (String)rMap.get("height");
String fillVal = (String)rMap.get("fill");
String strokeVal = (String)rMap.get("stroke");
rectToInt(xRect, yRect, rectWidth, rectHeight, fillVal, strokeVal);

}
NodeList lineList = svg.getElementsByTagName("line");

for(int i=0; i<lineList.getLength(); i++)
{
Node line = lineList.item(i);
NamedNodeMap lineMap = line.getAttributes();
HashMap lMap = new HashMap();

for(int iattr = 0; iattr < lineMap.getLength(); iattr++)
{
Node attr = lineMap.item(iattr);
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
//System.out.println("Attr: " + attrName);
//System.out.println("Value: " + attrValue);
lMap.put(attrName, attrValue);
//String x = (String)hMap.get("x");
//if (x != null)
//{
// System.out.println("x Attr" + x);
//}

}
String xLine1 = (String)lMap.get("x1");
String yLine1 = (String)lMap.get("y1");
String xLine2 = (String)lMap.get("x2");
String yLine2 = (String)lMap.get("y2");
String sWidth = (String)lMap.get("stroke-width");
lineToInt(xLine1, yLine1, xLine2, yLine2, sWidth);

}
}

public void rectToInt(String xRect, String yRect, String rectWidth, String rectHeight, String fillVal, String strokeVal)
{
//rectangle variables from file
int xR = Integer.parseInt(xRect);
System.out.println("Rectangle x: " + xR);
int yR = Integer.parseInt(yRect);
System.out.println("Rectangle y: " + yR);
int rWidth = Integer.parseInt(rectWidth);
System.out.println("Rectangle width: " + rWidth);
int rHeight = Integer.parseInt(rectHeight);
System.out.println("Rectangle height: " + rHeight);
String rFill = fillVal;
System.out.println("Rectangle fill: " + rFill);
String rStroke = strokeVal;
System.out.println("Rectangle stroke: " + rStroke);
c.add(new RectGraphics(xR, yR, rWidth, rHeight));
}

public void lineToInt(String xLine1, String yLine1, String xLine2, String yLine2, String sWidth)
{
//line variables from file
int xL1 = Integer.parseInt(xLine1);
System.out.println("Line x1: " + xL1);
int yL1 = Integer.parseInt(yLine1);
System.out.println("Line y1: " + yL1);
int xL2 = Integer.parseInt(xLine2);
System.out.println("Line x2: " + xL2);
int yL2 = Integer.parseInt(yLine2);
System.out.println("Line y2: " + yL2);
int sWidthLine = Integer.parseInt(sWidth);
System.out.println("Line Stroke-Width: " + sWidthLine);
c.add(new LineGraphics(xL1, yL1, xL2, yL2, sWidthLine));
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top