Reputation: 2491
I have two shapes and I want to do a zoom, I use the following code
Iterator iter = objects.iterator();
Shape shape;
while(iter.hasNext()){
shape = (Shape)iter.next();
AffineTransform t = shape.getAffineTransform();
int x = shape.getCenter().x;
int y = shape.getCenter().y;
t.translate(-x, -y);
t.scale(sx,sy);
t.translate(x, y);
shape.setAffineTransform(t);
}
The shapes are zoomed but the distance between them became smalled
I thought making a composite shape from the two shapes and then scale it. Is there another way to keep the proportions? thanks
Upvotes: 0
Views: 1756
Reputation: 2491
Thank you everybody for the answers, I found a solution that works for me
while(iter.hasNext()){
shape = (Shape)iter.next();
AffineTransform t = new AffineTransform();
t.scale(sx,sy);
t.concatenate(shape.getAffineTransform());
shape.setAffineTransform(t);
}
}
Upvotes: 0
Reputation: 1601
If you want to have a true zoom, that means scaling the shapes and the white space between them. If you follow the naive route (scale each shape, and then scale the distance from a shape to every other shape) the result will not be right.
One method I'm aware of is to treat the distance between the center of two shapes as a vector of "attracting" force that have fixed direction. Then scale the magnitude of those forces and find the points that will put he system in balance again. The maths should be out there, someplace. I saw that so long ago that it seems like it was in another life.
Upvotes: 0
Reputation: 49804
Like I said in my comment, because you scale the shapes around their respective centers, the centers will obviously not move around. If you want everything to be scaled uniformly, don't translate anything:
The best way to figure these transformations out is to grab pencil and paper, draw a coordinate system and follow the transformations on paper.
Upvotes: 2
Reputation: 26886
The code you have zooms each shape about its centre. This means the lower lines move down, and the upper lines move up. That's why they seem to move together.
In order to not have this happen you have to zoom the shapes about a point on the lower boundary (for the upper shape) and the upper boundary (for the lower shape). Make the "y" variable the appropriate boundary.
As an alternative (and equivalent) approach translate each shape by an appropriate amount after you have zoomed it.
Upvotes: 0