xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Raghunadha R Kotha" <ra...@verniernetworks.com>
Subject Can't display SVG document in JPanel
Date Mon, 09 Sep 2002 18:24:12 GMT
Hi,

I have this program, Doesn't throw any exception and doesn't display 
SVG document either. Can some one shed some light on this.

I am trying to display an SVG document in JPanel. It doesn't display SVG
document In the Panel.

What am I doing wrong?

Thanks in advance.
Raghu





import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import org.w3c.dom.svg.SVGAElement;
import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.InputSource;

import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.gvt.event.EventDispatcher;
import org.apache.batik.gvt.filter.ConcreteGraphicsNodeRableFactory;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.BridgeExtension;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.bridge.BridgeException;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.batik.dom.util.DOMUtilities.*;
import org.w3c.dom.svg.*;
import org.apache.batik.util.ParsedURL;
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
import java.awt.image.renderable.RenderContext;

public class SVGTagTest extends JFrame{

private SVGDocument doc;
//private Component canvas;
private JPanel canvas;
private Element svgElem;
private GraphicsNode graphicsNode;

public static void main(String[] args) throws Exception { SVGTagTest
test = new SVGTagTest(); }

public SVGTagTest() throws Exception {
JPanel mainPanel = new JPanel();
LayoutManager layout = new BorderLayout();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,600);
setVisible(true);
//canvas = getContentPane(); getContentPane().setLayout(layout);
mainPanel.setBackground(Color.white);
getContentPane().add(mainPanel, BorderLayout.CENTER);
canvas = mainPanel;
read(new FileReader("/Temp/henryV.svg"));
displaySvg();
repaint();
}

public void read(Reader is) throws Exception{
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
try {
String parser = 
XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory
(parser);
String uri = "http://...";
doc = (SVGDocument)f.createDocument(svgNS, is);
} catch (IOException ex) {
System.out.println("Can't load document");
}

/*
DOMImplementation impl = 
SVGDOMImplementation.getDOMImplementation();

doc = (SVGDocument)impl.createDocument(svgNS, "svg", null); InputSource
saxIs = new InputSource(is); DOMParser parser = new DOMParser();
parser.setIncludeIgnorableWhitespace(false);
parser.setFeature
("http://apache.org/xml/features/dom/create-entity-ref-nodes",false);
parser.parse(saxIs);
doc = (SVGDocument)parser.getDocument();
*/
}

public void displaySvgOld(){
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
svgElem = (Element)doc.getElementsByTagNameNS
(svgNS, "svg").item(0);
if (svgElem == null) return;
GVTBuilder b = new GVTBuilder();
Graphics2D g = (Graphics2D)canvas.getGraphics();
UserAgent ua = new NLMSVGUserAgent(canvas);
BridgeContext ctx = new BridgeContext(ua);
graphicsNode = b.build(ctx, svgElem); // exception here!
graphicsNode.paint(g); }

public void displaySvg(){
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
svgElem = (Element)doc.getElementsByTagNameNS
(svgNS, "svg").item(0);
if (svgElem == null) return;

UserAgent userAgent = new 
org.apache.batik.bridge.UserAgentAdapter();//new NLMSVGUserAgent
(canvas); // e.g. org.apache.batik.bridge.UserAgentAdapter
GVTBuilder builder = new GVTBuilder();
BridgeContext ctx = new BridgeContext(userAgent);
try {
graphicsNode = builder.build(ctx, doc);
} catch (BridgeException e){
System.out.println("Bridge Exception in DisplaySvg " + 
e.getMessage());

}
Graphics2D g = (Graphics2D)canvas.getGraphics(); graphicsNode.paint(g);
}

private SVGDocument doc2svgDoc (Document doc) {
SVGDocument svgDoc;
svgDoc = (SVGDocument)
org.apache.batik.dom.util.DOMUtilities.deepCloneDocument(doc, 
SVGDOMImplementation.getDOMImplementation());
return svgDoc;
}

}

class NLMSVGUserAgent extends UserAgentAdapter {

private Component component;
private AffineTransform transform;

public NLMSVGUserAgent(Component comp){
component = comp;
transform = new AffineTransform();
}

public Dimension2D getViewportSize() {
return component.getSize();
}

public EventDispatcher getEventDispatcher() {
return null;
}

public void displayError(String message) { System.out.println(message);
}

public void displayError(Exception ex) {
System.out.println(ex.getMessage());
}

public void displayMessage(String message) {
System.out.println(message); }

public float getPixelToMM() {
return 0.264583333333333333333f; // 96 dpi
}

public String getLanguages() {
return "en";
}

public String getUserStyleSheetURI() {
return null;
}

public void openLink(SVGAElement elt) {
}

protected void fireLinkActivatedEvent(SVGAElement elt, 
String href) {
}

public void setSVGCursor(Cursor cursor) {
//JSVGComponent.this.setCursor(cursor);
}

public String getXMLParserClassName() {
return "org.apache.crimson.parser.XMLReaderImpl";
}

public AffineTransform getTransform() {
return transform;
}

public Point getClientAreaLocationOnScreen() {
return component.getLocationOnScreen();
}

public boolean hasFeature(String s) {
return false;
}

protected Map extensions = new HashMap();

public boolean supportExtension(String s) {
return extensions.containsKey(s);
}

public void registerExtension(BridgeExtension ext) {
Iterator i = ext.getImplementedExtensions();
while (i.hasNext())
extensions.put(i.next(), ext);
}
}

Raghu
 
raghu@verniernetworks.com
Direct Line: (650) 428-0449
Fax: (650)237-2201
http://www.verniernetworks.com



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Mime
View raw message