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();
}
}