xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Selva <selvakumar_...@yahoo.co.in>
Subject Re: JSVGScrollPane's ScrollBar not updating properly
Date Fri, 15 Sep 2006 09:32:36 GMT

Hi Thomas,

To update the zoom in transform in canvas, i have translated the zoomed in
transform. but still there is some minor problem.

After zoomed in to large size, i moved the horizontal and vertical scroll
bars to right and bottom corner. Now if i tried to zoomout the document,
scroll bars not moving properly. Herewith i have attached the sample
application. Please let me know where i am doing the mistake.

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.JButton;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.DOMImplementation;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGSVGElement;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.Event;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.apache.batik.swing.JSVGScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.geom.AffineTransform;
import org.apache.batik.util.SVGConstants;
import org.apache.batik.bridge.ViewBox;
import javax.swing.JDialog;
import java.awt.geom.Rectangle2D;

class ExFrame extends JFrame{

    static String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    JSVGCanvas svgCanvas = new JSVGCanvas();
    JButton zoomInButton = new JButton("ZoomIn");
    JButton zoomOutButton = new JButton("ZoomOut");

    public static void main(String args[]) {
        ExFrame frame = new ExFrame();
        frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public ExFrame() {
        this.getContentPane().setLayout(new BorderLayout());

        JPanel jPanel1 = new JPanel();
        jPanel1.add(zoomInButton);
        jPanel1.add(zoomOutButton);

        zoomInButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AffineTransform rat = svgCanvas.getRenderingTransform();
                AffineTransform at = AffineTransform.getScaleInstance(1.1,
1.1);
                AffineTransform t = new AffineTransform(at);
                t.concatenate(rat);

                Rectangle2D viewBox = getViewBoxRect();
                AffineTransform vTx = svgCanvas.getViewBoxTransform();
                final Rectangle docSize =
vTx.createTransformedShape(viewBox).getBounds();
                Rectangle r2d =
t.createTransformedShape(viewBox).getBounds();

                int tx = 0, ty = 0;
                tx = docSize.x - r2d.x;
                ty = docSize.y - r2d.y;
                t.translate( -tx, -ty);
                svgCanvas.setRenderingTransform(t);
            }
        });

        zoomOutButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AffineTransform rat = svgCanvas.getRenderingTransform();
                AffineTransform at = AffineTransform.getScaleInstance(.9,
.9);
                AffineTransform t = new AffineTransform(at);
                t.concatenate(rat);
               
                Rectangle2D viewBox = getViewBoxRect();
                
                AffineTransform vTx = svgCanvas.getViewBoxTransform();
                final Rectangle docSize =
vTx.createTransformedShape(viewBox).getBounds();
                Rectangle r2d =
t.createTransformedShape(viewBox).getBounds();
                
                int tx = 0, ty = 0;
                tx = docSize.x - r2d.x;
                ty = docSize.y - r2d.y;
                t.translate( -tx, -ty);
                svgCanvas.setRenderingTransform(t);
            }
        });

        DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
        SVGDocument doc = (SVGDocument)impl.createDocument(svgNS, "svg",
null);
        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        JSVGScrollPane graphPane = new JSVGScrollPane(svgCanvas);
        svgCanvas.setDocument(doc);
        graphPane.setPreferredSize(new Dimension(400, 400));
        this.getContentPane().add(graphPane, java.awt.BorderLayout.CENTER);
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
        registerListeners();
    }

    public void registerListeners() {
        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
            }
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                resizeCircle();
            }
        });

        SVGDocument doc = svgCanvas.getSVGDocument();
        SVGSVGElement svgRoot = doc.getRootElement();
        EventTarget t = (EventTarget)svgRoot;
        t.addEventListener("SVGLoad", new OnLoadAction(), false);
    }

    public class OnLoadAction implements EventListener {
        public void handleEvent(Event evt) {
        }
    }

    protected Rectangle2D getViewBoxRect() {
        SVGSVGElement el = svgCanvas.getSVGDocument().getRootElement();
        String viewBoxStr = el.getAttributeNS(null,
SVGConstants.SVG_VIEW_BOX_ATTRIBUTE);
        float[] rect = ViewBox.parseViewBoxAttribute(el, viewBoxStr);
        return new Rectangle2D.Float(rect[0], rect[1], rect[2], rect[3]);
    }


    private void resizeCircle() throws DOMException {
        try {
            SVGDocument doc = svgCanvas.getSVGDocument();
            doc.getRootElement().setAttribute("width", "" + 100);
            doc.getRootElement().setAttribute("height", "" + 100);
            doc.getRootElement().setAttribute("viewBox", "0 0 " + 100 + " "
+ 100);
            doc.getRootElement().setAttribute("preserveAspectRatio",
"none");

            Element rect =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect.setAttributeNS(null, "id", "rec");
            rect.setAttributeNS(null, "x", "25");
            rect.setAttributeNS(null, "y", "25");
            rect.setAttributeNS(null, "width", "50");
            rect.setAttributeNS(null, "height", "50");
            rect.setAttributeNS(null, "style", "fill:red; stroke:green");
            doc.getRootElement().appendChild(rect);
        } catch (Exception e) {}
    }

}

Thanks in advance.

Regards,
Selva

-- 
View this message in context: http://www.nabble.com/JSVGScrollPane%27s-ScrollBar-not-updating-properly-tf2258957.html#a6322136
Sent from the Batik - Users forum at Nabble.com.


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


Mime
View raw message