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: Transform problem while zoom out
Date Sun, 17 Sep 2006 12:41:18 GMT

Hi Thomas,

>    Well I tried your application and I didn't see anything obvious
> that was wrong with the scroll bars.  I noticed that your code
> isn't set up to compile against trunk which leaves the question
> of what you are targeting???


Actually i was created that example with my old code base. So i haven't
remove the non supported codes by latest code base. Now i have updated it
with batik svn version.

Here my problem is i am zooming out the document to large scale. After that
i am moving the horizontal and vertical scroll bars to left and bottom
corners. Now if i tried to zoom out using "zoomout" button, document
translation is not proper. ScrollBars are supposed to stay in left and
bottom corners and document must display the corresponding translated
document. But here it is not translating properly.

For translation, i calculated the tx and ty values using ViewBoxTransform
and TransformedShape values. but it doesn't translate it properly. So can
you please guide me to calculate translation values.

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;
import org.apache.batik.bridge.UpdateManager; 

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,
null); 
        return new Rectangle2D.Float(0, 0, 100, 100); 
    } 


    private void resizeCircle() throws DOMException {
        
    try {
          final UpdateManager um = svgCanvas.getUpdateManager();
          um.getUpdateRunnableQueue().invokeLater(new Runnable() {
            public void run() {
              draw();
            }
          });
      } catch (Exception ex) {

      }

    }
    private void draw() throws DOMException { 
            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); 
    } 

} 

Thanks,
Selva




-- 
View this message in context: http://www.nabble.com/Transform-problem-while-zoom-out-tf2276756.html#a6349645
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