xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aaaa aaaa <scorefa...@yahoo.de>
Subject Re: Repainting a JSVGCanvas
Date Wed, 28 May 2008 09:02:22 GMT
not the resizing triggers the exception. The occurance of the exception seems completely arbitrary
to me and is not reproduced easily. 

I think it only occurs when I move the cursor over an svg element where the set attribute
is used so that the fill attribute changes. Resizing repaints the canvas. So my first (and
dirty) solution was to make this kind of repaint when the variable is null.

Of course it would be better when I would understand why I get this NullPointer Exception.

I have following classes (SVGArea and SVGAreaPainter) which displays the document. The SVGArea
is used by the SVGAreaPainter. The SVGAreaPainter has a lot methods. The one that makes the
mouseover effect for some elements is

	private void addMouseOverEffect(Element el, String color) {
		Element mouseOver = doc.createElementNS(svgNS, "set");
		mouseOver.setAttributeNS(null, "attributeName", "fill");
		mouseOver.setAttributeNS(null, "attributeType", "XML");
		mouseOver.setAttributeNS(null, "to", color);
		mouseOver.setAttributeNS(null, "begin", "mouseover");
		Element mouseOut = doc.createElementNS(svgNS, "set");
		mouseOut.setAttributeNS(null, "attributeName", "fill");
		mouseOut.setAttributeNS(null, "attributeType", "XML");
		mouseOut.setAttributeNS(null, "to", el.getAttribute("fill"));
		mouseOut.setAttributeNS(null, "begin", "mouseout");
SVGArea class:

package de.unidue.inf.xmlvis.gui.svg;

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.SwingUtilities;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererListener;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.svg.GVTTreeBuilderListener;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent;
import org.apache.batik.swing.svg.SVGLoadEventDispatcherListener;
import org.apache.log4j.Logger;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;

import de.unidue.inf.xmlvis.gui.StatusBarManager;

public class SVGArea extends JSVGCanvas implements SVGLoadEventDispatcherListener, GVTTreeRendererListener,
		MouseWheelListener {

	private static final long serialVersionUID = -4835732750510548844L;
	private static final Logger logger = Logger.getLogger(SVGArea.class);
	private static final String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
	private static final DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
	private int width;
	private int height;
	private Document doc = null;
	private Element svgRoot;
	private JSVGScrollPane scroll;

	public SVGArea() {
		super(null, true, false);
		doc = impl.createDocument(svgNS, "svg", null);
		svgRoot = doc.getDocumentElement();

	public void setDocument() {
		setSVGDocument((SVGDocument) doc);

	public Document getDocument() {
		return doc;

	public void resetSVGDocument() {
		doc = impl.createDocument(svgNS, "svg", null);
		svgRoot = doc.getDocumentElement();

	public void svgLoadEventDispatchCancelled(SVGLoadEventDispatcherEvent e) {

	public void svgLoadEventDispatchCompleted(SVGLoadEventDispatcherEvent e) {

	public void svgLoadEventDispatchFailed(SVGLoadEventDispatcherEvent e) {

	public void svgLoadEventDispatchStarted(SVGLoadEventDispatcherEvent e) {

	public void gvtRenderingCancelled(GVTTreeRendererEvent e) {

	public void gvtRenderingCompleted(GVTTreeRendererEvent e) {

	public void gvtRenderingFailed(GVTTreeRendererEvent e) {

	public void gvtRenderingPrepare(GVTTreeRendererEvent e) {

	public void gvtRenderingStarted(GVTTreeRendererEvent e) {

	public void gvtBuildCancelled(GVTTreeBuilderEvent e) {

	public void gvtBuildCompleted(GVTTreeBuilderEvent e) {

	public void gvtBuildFailed(GVTTreeBuilderEvent e) {

	public void gvtBuildStarted(GVTTreeBuilderEvent e) {

	public void setSVGSize(int width, int height) {
		this.width = width;
		this.height = height;
		svgRoot.setAttributeNS(null, "width", String.valueOf(width));
		svgRoot.setAttributeNS(null, "height", String.valueOf(height));
		SVGAreaPainter painter = new SVGAreaPainter(this);
		painter.drawText(0, 0, ".", 1);
		painter.drawText(width, height, ".", 1);

	public float getSVGWidth() {
		return width;

	public float getSVGHeight() {
		return height;

	public void mouseWheelMoved(final MouseWheelEvent e) {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				// An das passende ScrollPane weiterleiten
				if (scroll != null) {


	public void setScroll(JSVGScrollPane scroll) {
		this.scroll = scroll;

--- Cameron McCormack <cam@mcc.id.au> schrieb am Mi, 28.5.2008:
Von: Cameron McCormack <cam@mcc.id.au>
Betreff: Re: Repainting a JSVGCanvas
An: batik-users@xmlgraphics.apache.org
Datum: Mittwoch, 28. Mai 2008, 9:04

aaaa aaaa: > the problem is that I get occasionally a NullPointerException in the >
ScrollListener of the JSVGCanvas. The variable "newview" is null in > the updateCompleted
method .  Hmm.  I’m not sure what would cause getViewBoxRect() to return null if all you
are doing is resizing, unless there’s a threading issue.  > Rectangle2D newview = getViewBoxRect();
>  > So sometimes the canvas gets white and it has to be repaint. It's > immediatly
repainted when I resize the frame or when I'm using the > scrollbars. But I've inserted
> > if (newview == null) { > 				canvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() { >  > 					@Override > 					public void run() { > 						canvas.repaint();
> 					} >  > 				}); > 				return; > 			} >  > and this does not have
any effect. Another solution maybe would be to > avoid to get the Exception. But I don't
see what I'm doing wrong.  I don’t think calling canvas.repaint() is the right
 thing to fix the problem.  It sounds like there is a problem with JSVGScrollPane or JSVGCanvas.
 Are you able to provide a reduced test case that demonstrates the problem (perhaps just loading
a small SVG document in a JSVGScrollPane/JSVGCanvas, where continually resizing it will trigger
the bug)?  Thanks,  Cameron  --  Cameron McCormack ≝ http://mcc.id.au/  ---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org For additional commands,
e-mail: batik-users-help@xmlgraphics.apache.org

Gesendet von Yahoo! Mail.
Dem pfiffigeren Posteingang.

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

View raw message