i copied a code in
http://wiki.apache.org/xmlgraphics-batik/Bac
kgroundGlassPaneTutorial here
and tried playing with the code for practice and this is
what i have came
up:
*** CODE ***
import javax.swing.*;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.batik.swing.*;
import org.w3c.dom.events.*;
public class MapCanvas {
protected JSVGCanvas canvas;
protected JLabel target;
public MapCanvas(JPanel panel) {
panel.removeAll();
canvas = new JSVGCanvas();
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
canvas.setSize(400,400);
canvas.setDocument(buildDocument());
panel.add(canvas);
panel.repaint();
}
protected Document buildDocument() {
DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
String svgNS =
SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = impl.createDocument(svgNS,
"svg", null);
// get the root element (the svg element)
Element svgRoot = doc.getDocumentElement();
// set the width and height attribute on the root
svg element
svgRoot.setAttributeNS(null, "width",
"400");
svgRoot.setAttributeNS(null, "height",
"400");
// create the array of rectangles
Element g = doc.createElementNS(svgNS,
"g");
g.setAttributeNS(null, "id",
"rectangles");
for (int x = 10; x < 200 ; x += 30) {
for (int y = 10; y < 200; y += 30) {
Element rectangle =
doc.createElementNS(svgNS, "rect");
rectangle.setAttributeNS(null,
"x", "" + x);
rectangle.setAttributeNS(null,
"y", "" + y);
rectangle.setAttributeNS(null,
"width", "20");
rectangle.setAttributeNS(null,
"height", "20");
rectangle.setAttributeNS(null,
"style", "fill:blue");
rectangle.setAttributeNS(null,
"id", "rect: " + x + ", "+
y);
// attach the rectangle to the g element
g.appendChild(rectangle);
}
}
svgRoot.appendChild(g);
Element g2 = doc.createElementNS(svgNS,
"g");
g.setAttributeNS(null, "id",
"circles");
for (int x = 210; x < 400 ; x += 30) {
for (int y = 210; y < 400; y += 30) {
Element circle = doc.createElementNS(svgNS,
"circle");
circle.setAttributeNS(null, "cx",
"" + x);
circle.setAttributeNS(null, "cy",
"" + y);
circle.setAttributeNS(null, "r",
"10");
circle.setAttributeNS(null,
"style", "fill:red");
circle.setAttributeNS(null, "id",
"circle: " + x + ", "+ y);
// attach the rectangle to the g element
g2.appendChild(circle);
}
}
svgRoot.appendChild(g2);
return doc;
}
public void addGlassPane() {
String svgNS =
SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = canvas.getSVGDocument();
Element rectangle = doc.createElementNS(svgNS,
"rect");
rectangle.setAttributeNS(null, "x",
"0");
rectangle.setAttributeNS(null, "y",
"0");
rectangle.setAttributeNS(null, "width",
"400");
rectangle.setAttributeNS(null, "height",
"400");
rectangle.setAttributeNS(null, "style",
"fill:none;pointer-events:fill");
rectangle.setAttributeNS(null, "id",
"glasspane");
Element svgRoot = doc.getDocumentElement();
svgRoot.insertBefore(rectangle,
doc.getElementById("rectangles"));
}
public void registerListeners(JLabel target) {
//this label provides feedback on the selected item
this.target = target;
// Gets an element from the loaded document.
// document is your SVGDocument
Element elt =
canvas.getSVGDocument().getElementById("glasspane"
);
EventTarget t = (EventTarget)elt;
t.addEventListener("click", new
GlassPaneClick(), false);
Element elt2 =
canvas.getSVGDocument().getElementById("rectangles"
;);
EventTarget t2 = (EventTarget)elt2;
t2.addEventListener("click", new
ObjectClick(), false);
Element elt3 =
canvas.getSVGDocument().getElementById("circles");
EventTarget t3 = (EventTarget)elt3;
t3.addEventListener("click", new
CircleClick(), false);
}
public class GlassPaneClick implements EventListener {
public void handleEvent(Event evt) {
target.setText("Glasspane event
"+((Element)evt.getTarget()).getAttribute("id"
;));
target.repaint();
}
}
public class ObjectClick implements EventListener {
public void handleEvent(Event evt) {
target.setText("Rectangles event
"+((Element)evt.getTarget()).getAttribute("id"
;));
target.repaint();
}
}
public class CircleClick implements EventListener {
public void handleEvent(Event evt) {
target.setText("Circles event
"+((Element)evt.getTarget()).getAttribute("id"
;));
target.repaint();
}
}
}
*** END OF CODE ***
i don't know what's wrong. i get the error
java.lang.NullPointerException in
the line:
t2.addEventListener("click", new
ObjectClick(), false);
why am i having these errors?
--
View this message in context: http://www.nabble.com/batik-confusion...-tf3681
701.html#a10346772
Sent from the Batik - Users mailing list archive at
Nabble.com.
------------------------------------------------------------
---------
To unsubscribe, e-mail: batik-users-unsubscribe xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help xmlgraphics.apache.org
|