xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 天才 <p...@hotmail.com>
Subject [Help with Centering!!!]
Date Fri, 07 Nov 2008 13:35:59 GMT

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
object.
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...

best,
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);
        
        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();
        
        at.translate(offsetX/at.getScaleX(), offsetY/at.getScaleY());
        
        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);
    }
    
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new RotationTest();
            }
        });
    }
    
}

_________________________________________________________________
MSN热搜榜,每天最In的信息资讯和热点排行让您一览无余!
 http://top.msn.com.cn
Mime
View raw message