Hi,

still pending is the problem of resizing a JSVGCanvas. I attached the code for a very simple whiteboard to draw lines. After resizing, batik rendering get's strange behavior. Sometimes, on smaller window, parts get rendered doubled on the downside or cheesy background stays somehow. Sometimes rendering stucks completely although the UpdateManager works. Making the window big again, somehow magically repairs the rendering. Please, help me! I tried thousends of ugly workarrounds, some do work like changing and resetting the SVGDocument but lead to bad performance and flickering.

System.setProperty("sun.awt.noerasebackground", "true"), SWT.NO_REDRAW_RESIZE, SWT.NO_BACKGROUND are not connected to the problem.

Regards
Michael

Here see the attached code:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.AbstractJSVGComponent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;

public class SimpleWhiteboard extends Composite {

    static {
        System.setProperty("sun.awt.noerasebackground", "true");
    }

    public SimpleWhiteboard(Composite parent, int mode) {
        super(parent, mode);

        this.setLayout(new FillLayout());
        new SimpleSVGSurface(this);

    }

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Whiteboard");
        shell.setLayout(new FillLayout());
        SimpleWhiteboard instance = new SimpleWhiteboard(shell,
            SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        instance.dispose();
    }

    public static class SimpleSVGSurface extends Composite {

        private JSVGCanvas svgCanvas = null;
        private Frame frame = null;

        private Document svgDocument = null;

        public SimpleSVGSurface(Composite parent) {
            this(parent, SWT.NONE);
        }

        public SimpleSVGSurface(Composite parent, int mode) {
            super(parent, mode | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE
                | SWT.EMBEDDED);

            init();
            initXMLGraphics();
            initListeners();

        }

        protected void init() {

            svgCanvas = new JSVGCanvas();

svgCanvas.setDocumentState(AbstractJSVGComponent.ALWAYS_DYNAMIC);
            svgCanvas.setLayout(new BorderLayout());
            svgCanvas.setDoubleBuffered(true);
            svgCanvas.setDoubleBufferedRendering(true);

            frame = SWT_AWT.new_Frame(this);
            frame.setLayout(new BorderLayout());
            frame.add(BorderLayout.CENTER, svgCanvas);
            frame.setEnabled(true);

            frame.pack();
            frame.setVisible(true);
        }

        protected void initXMLGraphics() {
            DOMImplementation impl = SVGDOMImplementation
                .getDOMImplementation();
            String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

            svgDocument = impl.createDocument(svgNS, "svg", null);

            SVGGraphics2D svgGraphics = new SVGGraphics2D(svgDocument);
            // fill the SVG Document with the Defaults
            Element root = svgGraphics
                .getRoot(svgDocument.getDocumentElement());

            root.setAttributeNS(null, "overflow", "visible");

            svgCanvas.setDocument(svgDocument);
        }

        private static Point lastPoint = null;

        protected void initListeners() {
            svgCanvas.addMouseListener(new java.awt.event.MouseListener() {

                public void mouseClicked(java.awt.event.MouseEvent e) {
                    //
                }

                public void mouseEntered(java.awt.event.MouseEvent e) {
                    // TODO Auto-generated method stub

                }

                public void mouseExited(java.awt.event.MouseEvent e) {
                    //

                }

                public void mousePressed(java.awt.event.MouseEvent e) {
                    System.out.print("MouseDown ");
                    if (lastPoint == null)
                        lastPoint = e.getPoint();

                    appendLine(lastPoint, e.getPoint());
                    lastPoint = e.getPoint();
                }

                public void mouseReleased(java.awt.event.MouseEvent e) {
                    //
                }

            });

            svgCanvas
.addMouseMotionListener(new java.awt.event.MouseMotionListener() {

                    public void mouseDragged(java.awt.event.MouseEvent e) {
                        System.out.print("MouseDragged ");

                        appendLine(lastPoint, e.getPoint());
                        lastPoint = e.getPoint();

                    }

                    public void mouseMoved(java.awt.event.MouseEvent e) {
                        // System.out.println("MouseMoved");

                    }

                });

            initDebugListener();
        }

        private void appendLine(final Point start, final Point end) {

svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(
                new Runnable() {

                    public void run() {
                        System.out.println("WM works");

                        SVGDocument doc = svgCanvas.getSVGDocument();
String SVGNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

                        Element g = doc.createElementNS(SVGNS, "g");

                        Element e = doc.createElementNS(SVGNS, "line");

                        e.setAttributeNS(null, "fill", "none");
                        e.setAttributeNS(null, "x1", String.valueOf(start
                            .getX()));
                        e.setAttributeNS(null, "y1", String.valueOf(start
                            .getY()));
                        e
                            .setAttributeNS(null, "x2", String.valueOf(end
                                .getX()));
                        e
                            .setAttributeNS(null, "y2", String.valueOf(end
                                .getY()));

                        g.appendChild(e);

                        doc.getRootElement().appendChild(g);
                    }
                });
        }

        @Override
        public void dispose() {
            // should be disposed by the AWT thread to avoid deadlocks
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    svgCanvas.dispose();
                    frame.dispose();
                }
            });
            super.dispose();
        }

        private void initDebugListener() {
            svgCanvas.addComponentListener(new ComponentListener() {

                public void componentHidden(ComponentEvent arg0) {
                    // TODO Auto-generated method stub

                }

                public void componentMoved(ComponentEvent arg0) {
                    // TODO Auto-generated method stub

                }

                public void componentResized(ComponentEvent arg0) {

                    System.out.println("Resize: " + svgCanvas.getSize());

                }

                public void componentShown(ComponentEvent arg0) {
                    // TODO Auto-generated method stub

                }

            });

        }
    }

}

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

Reply via email to