xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 天才 <p...@hotmail.com>
Subject RE: [Help with Centering!!!]
Date Sun, 09 Nov 2008 10:08:29 GMT

Thanks Michael, it helped a lot 

Subject: RE: [Help with Centering!!!]
Date: Fri, 7 Nov 2008 11:01:43 -0500
From: michael.bishop.ctr@jfcom.mil
To: batik-users@xmlgraphics.apache.org

In order to properly do this, you must remove/undo the rotation, translate, then re-apply
the rotation angle.
    public static double getRotationAngle(AffineTransform transform) {
        // Eliminate any post-translation
        final AffineTransform cloned = (AffineTransform)transform.clone();
          -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;
double angle = getRotationAngle(renderingTransform);
renderingTransform.rotate(-angle, centerX, centerY);
renderingTransform.translate(offsetX, offsetY);
renderingTrasform.rotate(angle, centerX, centerY);
Michael Bishop

From: 天才 [mailto:p921@hotmail.com]
Sent: Fri 11/7/2008 8:35 AM
To: batik-users@xmlgraphics.apache.org
Subject: [Help with Centering!!!]

Dear Batik-Experts, 

I am programming an application using batik jsvgcanvas, but I've got a big big big problem
by implementing a centering function.
The sample codes are listed below, could someone please load it and point out what's wrong???
The codes below has a rectangle as the background and a small circle as the to be centered
If the button is pressed, the cicle should be placed in the center of the window.
It works very well if I didn't rotate (using ctrl+rightmouse) the whole. but works quite strange
if I did...



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;

im port 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(){
   &n bsp;    
        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.canvas.setMySize(new Dimension(bgWidth+2*offset, bgHeight+2*offset));
        this.getContentPane().add(this.canvas, "Center");
    private void initCenterBtn() {
        JButton centerBtn = new JButton("Center");
        centerBtn.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent arg0) {
        this.getContentPane().add(centerBtn, "North");

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

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

        at.transform(circleTemp, circleTemp);
        double offsetX = this.canvas.getWidth()/2-circleTemp.getX();
   &nb sp;    double offsetY = this.canvas.getHeight()/2-circleTemp.getY();
        at.translate(offsetX/at.getScaleX(), offsetY/at.getScaleY());
        }catch(Exception e){
    private void initBackGround(){
        Element bgE = svgDoc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, SVGConstants.SVG_RECT_TAG);
        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);
        ballE.setAttributeNS(null, SVGConstants.SVG_CX_ATTRIBUTE, ""+circleX);
        ballE.setAttributeNS(null, SVGConstants.SVG_CY_ATTRIBUTE, ""+circleY);
 & nbsp;      ballE.setAttributeNS(null, SVGConstants.SVG_R_ATTRIBUTE, ""+offset);
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new RotationTest();

更多热辣资讯尽在新版MSN首页! 立刻访问! 
View raw message