You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Cui Jian <ke...@informatik.uni-bremen.de> on 2008/12/18 18:34:14 UTC

[Help with Centering!!!]

Dear Batik-Experts,

weeks ago I asked the question about how I can center a certain point in 
a given svg-file, it worked very well with the answer provided by Michael.
But after I added the viewbox attribute to the svg-file today, I 
suddenly found the centering not working properly again (at least after 
I resize the gui with maximize for instance)...
I couldn't understand why it's not working, so could someone please take 
a look at the codes below and tell me why or how I can fix this?

best regards,
Cui

package rotationTest;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.SVGConstants;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class RotationTest extends JFrame{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private JSVGCanvas canvas;
   
    private Document svgDoc;
   
    private static final int offset = 20;
    private static final int bgWidth = 200;
    private static final int bgHeight = 300;
    private static final int circleX = 80;
    private static final int circleY = 100;
   
    public RotationTest(){
       
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        this.getContentPane().setLayout(new BorderLayout());
       
        this.canvas = new JSVGCanvas();
        this.canvas.setDocumentState (JSVGCanvas.ALWAYS_DYNAMIC);
       
        DOMImplementation dom = 
SVGDOMImplementation.getDOMImplementation ();
        svgDoc =
            dom.createDocument(SVGConstants.SVG_NAMESPACE_URI, 
SVGConstants.SVG_SVG_TAG, null);
       
        this.initBackGround();
        this.initBall();
       
        this.initCenterBtn();
       
        this.canvas.setDocument(svgDoc);
       
        Element rootE = svgDoc.getDocumentElement();
       
        rootE.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE, 
bgWidth+2*offset+"");
        rootE.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE, 
bgHeight+2*offset+"");
        rootE.setAttributeNS(null, SVGConstants.SVG_VIEW_BOX_ATTRIBUTE, 
"0 0 "+(bgWidth+2*offset)+" "+(bgHeight+2*offset));

        this.canvas.setMySize(new Dimension(bgWidth+2*offset, 
bgHeight+2*offset));
       
        this.getContentPane().add(this.canvas, "Center");
       
        this.pack();
        this.setVisible(true);
       
    }
   
    private void initCenterBtn() {
        JButton centerBtn = new JButton("Center");
        centerBtn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                center();
            }
        });
        this.getContentPane().add(centerBtn, "North");
    }

    private void center(){
        try{
        AffineTransform at = this.canvas.getRenderingTransform();

        Point2D circleTemp = new Point2D.Double(circleX, circleY);

        at.transform(circleTemp, circleTemp);
               
        double offsetX = this.canvas.getWidth()/2-circleTemp.getX();
        double offsetY = this.canvas.getHeight()/2-circleTemp.getY();
       
        double angle = getRotationAngle(at);
        at.rotate(-angle);
        at.translate(offsetX/at.getScaleX(), offsetY/at.getScaleY());
        at.rotate(angle);
       
        this.canvas.setRenderingTransform(at);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
   
    private void initBackGround(){
        Element bgE = 
svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 
SVGConstants.SVG_RECT_TAG);
        svgDoc.getDocumentElement().appendChild(bgE);
        bgE.setAttributeNS(null, SVGConstants.SVG_FILL_ATTRIBUTE, 
"lightblue");
        bgE.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE, ""+offset);
        bgE.setAttributeNS(null, SVGConstants.SVG_Y_ATTRIBUTE, ""+offset);
        bgE.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE, 
""+bgWidth);
        bgE.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE, 
""+bgHeight);
    }
   
    private void initBall(){
        Element ballE = 
svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 
SVGConstants.SVG_CIRCLE_TAG);
        svgDoc.getDocumentElement().appendChild(ballE);
        ballE.setAttributeNS(null, SVGConstants.SVG_CX_ATTRIBUTE, 
""+circleX);
        ballE.setAttributeNS(null, SVGConstants.SVG_CY_ATTRIBUTE, 
""+circleY);
        ballE.setAttributeNS(null, SVGConstants.SVG_R_ATTRIBUTE, ""+offset);
    }
   
    private static double getRotationAngle(AffineTransform transform) {
        // Eliminate any post-translation
        final AffineTransform cloned = (AffineTransform)transform.clone();
        cloned.preConcatenate(AffineTransform.getTranslateInstance(
          -transform.getTranslateX(), -transform.getTranslateY()));
        // Apply transformation to a vector
        final Point2D firstVector = new Point2D.Double(1, 0);
        final Point2D secondVector = cloned.transform(firstVector, null);
        // Compute dot product
        final double dotProduct = firstVector.getX() * secondVector.getX() +
                firstVector.getY() * secondVector.getY();
        // Compute positive angle
        double angle = Math.acos(dotProduct
          / (firstVector.distance(0, 0) * secondVector.distance(0, 0)));
        // Negate angle if rotation direction is clockwise
        if (secondVector.getY() < 0) {
            angle = -angle;
        }
        // Done
        return angle;
    }

   
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new RotationTest();
            }
        });
    }
   
}


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


Re: [Help with Centering!!!]

Posted by Cui Jian <ke...@informatik.uni-bremen.de>.
Hi Thomas,

thanks very much!
although I need some time to understand the whole, but it works :-)

best regards,
Cui
>
> Hi Cui,
>
> > > Dear Batik-Experts,
> > >
> > > weeks ago I asked the question about how I can center a certain point
> > > in a given svg-file, it worked very well with the answer provided by
> > > Michael.
> > > But after I added the viewbox attribute to the svg-file today, I
> > > suddenly found the centering not working properly again (at least
> > > after I resize the gui with maximize for instance)...
> > > I couldn't understand why it's not working, so could someone please
> > > take a look at the codes below and tell me why or how I can fix this?
>
> >>    private void center(){
> >>        try{
> >>        AffineTransform at = this.canvas.getRenderingTransform();
>
>     The major problem is that you want the ViewBox transform not
> just the rendering transform, they are the same... unless you have
> a viewBox.
>
> >>        Point2D circleTemp = new Point2D.Double(circleX, circleY);
> >>
> >>        at.transform(circleTemp, circleTemp);
> >>        double offsetX = this.canvas.getWidth()/2-circleTemp.getX();
> >>        double offsetY = this.canvas.getHeight()/2-circleTemp.getY();
>
>     At this point you need to get the RenderingTransform for at since
> that is what you want to update.  The stuff with the rotation is done to
> map the delta to the rendering transform.  This will work unless there
> is non-uniform scaling/shearing (which is unusual).  My version at the
> end doesn't have this small limitation.
>
> >>        double angle = getRotationAngle(at);
> >>        at.rotate(-angle);
> >>        at.translate(offsetX/at.getScaleX(), offsetY/at.getScaleY());
> >>        at.rotate(angle);
> >>        this.canvas.setRenderingTransform(at);
> >>        }catch(Exception e){
> > >            e.printStackTrace();
> > >        }
> > >    }
>
>      private void center(){
>        try{
>         AffineTransform at = this.canvas.getViewBoxTransform();
>
>         Point2D circleTemp = new Point2D.Double(circleX, circleY);
>
>         at.transform(circleTemp, circleTemp);
>        
>         double offsetX = this.canvas.getWidth()/2-circleTemp.getX();
>         double offsetY = this.canvas.getHeight()/2-circleTemp.getY();
>
>         at = this.canvas.getRenderingTransform();
>         AffineTransform iat = at.createInverse();
>
>         Point2D delta = new Point2D.Double(offsetX, offsetY);
>         iat.transform(delta, delta);
>        
>         at.translate(delta.getX()-iat.getTranslateX(),
>                      delta.getY()-iat.getTranslateY());
>        
>         this.canvas.setRenderingTransform(at);
>         }catch(Exception e){
>            e.printStackTrace();
>        }
>    }


-- 
Dipl. Inform. Cui(Ken) Jian 	

German Research Center for Artificial Intelligence
Safe and Secure Cognitive Systems
DFKI-Labor Bremen          	Universitaet Bremen, FB3
Sichere Kognitive Systeme  	Cartesium 0.051
Robert-Hooke-Str. 5        	Enrique-Schmidt-Str. 5
28359 Bremen               	28359 Bremen
http://www.dfki.de         	www.informatik.uni-bremen.de/~ken
Tel.: +49 (421) 218-64204
Telefax: +49 (421) 218-98-64204
E-Mail:  
cui.jian@dfki.de                ken@informatik.uni-bremen.de
-------------------------------------------------------------
Deutsches Forschungszentrum fuer Kuenstliche Intelligenz GmbH
Firmensitz: Trippstadter Strasse 122, D-67663 Kaiserslautern
Geschaeftsfuehrung:
Prof. Dr. Dr. h.c. mult. Wolfgang Wahlster (Vorsitzender)
Dr. Walter Olthoff
Vorsitzender des Aufsichtsrats:
Prof. Dr. h.c. Hans A. Aukes
Amtsgericht Kaiserslautern, HRB 2313
-------------------------------------------------------------


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


Re: [Help with Centering!!!]

Posted by th...@kodak.com.
Hi Cui,

> > Dear Batik-Experts,
> >
> > weeks ago I asked the question about how I can center a certain point 
> > in a given svg-file, it worked very well with the answer provided by 
> > Michael.
> > But after I added the viewbox attribute to the svg-file today, I 
> > suddenly found the centering not working properly again (at least 
> > after I resize the gui with maximize for instance)...
> > I couldn't understand why it's not working, so could someone please 
> > take a look at the codes below and tell me why or how I can fix this?

>>    private void center(){
>>        try{
>>        AffineTransform at = this.canvas.getRenderingTransform();

    The major problem is that you want the ViewBox transform not
just the rendering transform, they are the same... unless you have 
a viewBox.

>>        Point2D circleTemp = new Point2D.Double(circleX, circleY);
>>
>>        at.transform(circleTemp, circleTemp);
>>        double offsetX = this.canvas.getWidth()/2-circleTemp.getX();
>>        double offsetY = this.canvas.getHeight()/2-circleTemp.getY();

    At this point you need to get the RenderingTransform for at since 
that is what you want to update.  The stuff with the rotation is done to
map the delta to the rendering transform.  This will work unless there
is non-uniform scaling/shearing (which is unusual).  My version at the
end doesn't have this small limitation.

>>        double angle = getRotationAngle(at);
>>        at.rotate(-angle);
>>        at.translate(offsetX/at.getScaleX(), offsetY/at.getScaleY());
>>        at.rotate(angle);
>>        this.canvas.setRenderingTransform(at);
>>        }catch(Exception e){
> >            e.printStackTrace();
> >        }
> >    }

     private void center(){
        try{
        AffineTransform at = this.canvas.getViewBoxTransform();

        Point2D circleTemp = new Point2D.Double(circleX, circleY);

        at.transform(circleTemp, circleTemp);
 
        double offsetX = this.canvas.getWidth()/2-circleTemp.getX();
        double offsetY = this.canvas.getHeight()/2-circleTemp.getY();

        at = this.canvas.getRenderingTransform();
        AffineTransform iat = at.createInverse();

        Point2D delta = new Point2D.Double(offsetX, offsetY);
        iat.transform(delta, delta);
 
        at.translate(delta.getX()-iat.getTranslateX(), 
                     delta.getY()-iat.getTranslateY());
 
        this.canvas.setRenderingTransform(at);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

Re: [Help with Centering!!!]

Posted by Cui Jian <ke...@informatik.uni-bremen.de>.
Hi All,

I've asked the question for months, but got no reply until now... maybe 
it's a bug of Batik???

best,
Cui
> Dear Batik-Experts,
>
> weeks ago I asked the question about how I can center a certain point 
> in a given svg-file, it worked very well with the answer provided by 
> Michael.
> But after I added the viewbox attribute to the svg-file today, I 
> suddenly found the centering not working properly again (at least 
> after I resize the gui with maximize for instance)...
> I couldn't understand why it's not working, so could someone please 
> take a look at the codes below and tell me why or how I can fix this?
>
> best regards,
> Cui
>
> package rotationTest;
>
> import java.awt.BorderLayout;
> import java.awt.Dimension;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import java.awt.geom.AffineTransform;
> import java.awt.geom.Point2D;
>
> import javax.swing.JButton;
> import javax.swing.JFrame;
> import javax.swing.SwingUtilities;
>
> import org.apache.batik.dom.svg.SVGDOMImplementation;
> import org.apache.batik.swing.JSVGCanvas;
> import org.apache.batik.util.SVGConstants;
> import org.w3c.dom.DOMImplementation;
> import org.w3c.dom.Document;
> import org.w3c.dom.Element;
>
> public class RotationTest extends JFrame{
>
>    /**
>     *
>     */
>    private static final long serialVersionUID = 1L;
>
>    private JSVGCanvas canvas;
>      private Document svgDoc;
>      private static final int offset = 20;
>    private static final int bgWidth = 200;
>    private static final int bgHeight = 300;
>    private static final int circleX = 80;
>    private static final int circleY = 100;
>      public RotationTest(){
>              this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>              this.getContentPane().setLayout(new BorderLayout());
>              this.canvas = new JSVGCanvas();
>        this.canvas.setDocumentState (JSVGCanvas.ALWAYS_DYNAMIC);
>              DOMImplementation dom = 
> SVGDOMImplementation.getDOMImplementation ();
>        svgDoc =
>            dom.createDocument(SVGConstants.SVG_NAMESPACE_URI, 
> SVGConstants.SVG_SVG_TAG, null);
>              this.initBackGround();
>        this.initBall();
>              this.initCenterBtn();
>              this.canvas.setDocument(svgDoc);
>              Element rootE = svgDoc.getDocumentElement();
>              rootE.setAttributeNS(null, 
> SVGConstants.SVG_WIDTH_ATTRIBUTE, bgWidth+2*offset+"");
>        rootE.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE, 
> bgHeight+2*offset+"");
>        rootE.setAttributeNS(null, SVGConstants.SVG_VIEW_BOX_ATTRIBUTE, 
> "0 0 "+(bgWidth+2*offset)+" "+(bgHeight+2*offset));
>
>        this.canvas.setMySize(new Dimension(bgWidth+2*offset, 
> bgHeight+2*offset));
>              this.getContentPane().add(this.canvas, "Center");
>              this.pack();
>        this.setVisible(true);
>          }
>      private void initCenterBtn() {
>        JButton centerBtn = new JButton("Center");
>        centerBtn.addActionListener(new ActionListener(){
>            public void actionPerformed(ActionEvent arg0) {
>                center();
>            }
>        });
>        this.getContentPane().add(centerBtn, "North");
>    }
>
>    private void center(){
>        try{
>        AffineTransform at = this.canvas.getRenderingTransform();
>
>        Point2D circleTemp = new Point2D.Double(circleX, circleY);
>
>        at.transform(circleTemp, circleTemp);
>                      double offsetX = 
> this.canvas.getWidth()/2-circleTemp.getX();
>        double offsetY = this.canvas.getHeight()/2-circleTemp.getY();
>              double angle = getRotationAngle(at);
>        at.rotate(-angle);
>        at.translate(offsetX/at.getScaleX(), offsetY/at.getScaleY());
>        at.rotate(angle);
>              this.canvas.setRenderingTransform(at);
>        }catch(Exception e){
>            e.printStackTrace();
>        }
>    }
>      private void initBackGround(){
>        Element bgE = 
> svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 
> SVGConstants.SVG_RECT_TAG);
>        svgDoc.getDocumentElement().appendChild(bgE);
>        bgE.setAttributeNS(null, SVGConstants.SVG_FILL_ATTRIBUTE, 
> "lightblue");
>        bgE.setAttributeNS(null, SVGConstants.SVG_X_ATTRIBUTE, ""+offset);
>        bgE.setAttributeNS(null, SVGConstants.SVG_Y_ATTRIBUTE, ""+offset);
>        bgE.setAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE, 
> ""+bgWidth);
>        bgE.setAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE, 
> ""+bgHeight);
>    }
>      private void initBall(){
>        Element ballE = 
> svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 
> SVGConstants.SVG_CIRCLE_TAG);
>        svgDoc.getDocumentElement().appendChild(ballE);
>        ballE.setAttributeNS(null, SVGConstants.SVG_CX_ATTRIBUTE, 
> ""+circleX);
>        ballE.setAttributeNS(null, SVGConstants.SVG_CY_ATTRIBUTE, 
> ""+circleY);
>        ballE.setAttributeNS(null, SVGConstants.SVG_R_ATTRIBUTE, 
> ""+offset);
>    }
>      private static double getRotationAngle(AffineTransform transform) {
>        // Eliminate any post-translation
>        final AffineTransform cloned = (AffineTransform)transform.clone();
>        cloned.preConcatenate(AffineTransform.getTranslateInstance(
>          -transform.getTranslateX(), -transform.getTranslateY()));
>        // Apply transformation to a vector
>        final Point2D firstVector = new Point2D.Double(1, 0);
>        final Point2D secondVector = cloned.transform(firstVector, null);
>        // Compute dot product
>        final double dotProduct = firstVector.getX() * 
> secondVector.getX() +
>                firstVector.getY() * secondVector.getY();
>        // Compute positive angle
>        double angle = Math.acos(dotProduct
>          / (firstVector.distance(0, 0) * secondVector.distance(0, 0)));
>        // Negate angle if rotation direction is clockwise
>        if (secondVector.getY() < 0) {
>            angle = -angle;
>        }
>        // Done
>        return angle;
>    }
>
>      public static void main(String[] args){
>        SwingUtilities.invokeLater(new Runnable() {
>            public void run() {
>                new RotationTest();
>            }
>        });
>    }
>   }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org



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