Thanks Michael, it helped a lot
Subject: RE: [Help with Centering!!!]
Date: Fri, 7 Nov 2008 11:01:43 -0500
From: [EMAIL PROTECTED]
To: [email protected]
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();
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;
}
double angle = getRotationAngle(renderingTransform);
renderingTransform.rotate(-angle, centerX, centerY);
renderingTransform.translate(offsetX, offsetY);
renderingTrasform.rotate(angle, centerX, centerY);
Michael Bishop
From: 天才 [mailto:[EMAIL PROTECTED]
Sent: Fri 11/7/2008 8:35 AM
To: [email protected]
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 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;
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.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();
&nb sp; 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);
& 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首页! 立刻访问!
_________________________________________________________________
超炫人气榜给您所有偶像的最新资讯和排名,快来支持自己的偶像!
http://cnweb.search.live.com/xrank/results.aspx?q=%e5%91%a8%e6%9d%b0%e4%bc%a6&FORM=MSNH