« Return to Thread: Repainting a JSVGCanvas

Re: Repainting a JSVGCanvas

by aaaa aaaa :: Rate this Message:

Reply to Author | View in Thread

Hi,
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");
                el.appendChild(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");
                el.appendChild(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, GVTTreeBuilderListener,
                MouseWheelListener {

        private static final long serialVersionUID = -4835732750510548844L;
        @SuppressWarnings("unused")
        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);
                addSVGLoadEventDispatcherListener(this);
                addGVTTreeRendererListener(this);
                addGVTTreeBuilderListener(this);
                addMouseWheelListener(this);
                setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
                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();
        }

        @Override
        public void svgLoadEventDispatchCancelled(SVGLoadEventDispatcherEvent e) {
                StatusBarManager.svgLoadEventDispatchCancelled();
        }

        @Override
        public void svgLoadEventDispatchCompleted(SVGLoadEventDispatcherEvent e) {
        }

        @Override
        public void svgLoadEventDispatchFailed(SVGLoadEventDispatcherEvent e) {
                StatusBarManager.svgLoadEventDispatchFailed();
        }

        @Override
        public void svgLoadEventDispatchStarted(SVGLoadEventDispatcherEvent e) {
                StatusBarManager.svgLoadEventDispatchStarted();
        }

        @Override
        public void gvtRenderingCancelled(GVTTreeRendererEvent e) {
        }

        @Override
        public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                StatusBarManager.gvtRenderingCompleted();
        }

        @Override
        public void gvtRenderingFailed(GVTTreeRendererEvent e) {
        }

        @Override
        public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
        }

        @Override
        public void gvtRenderingStarted(GVTTreeRendererEvent e) {
        }

        @Override
        public void gvtBuildCancelled(GVTTreeBuilderEvent e) {
        }

        @Override
        public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
        }

        @Override
        public void gvtBuildFailed(GVTTreeBuilderEvent e) {
        }

        @Override
        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;
        }

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

                        @Override
                        public void run() {
                                // An das passende ScrollPane weiterleiten
                                if (scroll != null) {
                                        scroll.dispatchEvent(e);
                                }
                        }

                });

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

--- Cameron McCormack <cam@...> schrieb am Mi, 28.5.2008:
Von: Cameron McCormack <cam@...>
Betreff: Re: Repainting a JSVGCanvas
An: batik-users@...
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@... For additional commands, e-mail: batik-users-help@...


      __________________________________________________________
Gesendet von Yahoo! Mail.
Dem pfiffigeren Posteingang.
http://de.overview.mail.yahoo.com

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

 « Return to Thread: Repainting a JSVGCanvas