You are printing the size and scale from local variables obtained before applying the transform. Try getting the actual values from the shape instead.
Java Affine Transform Scale()
-
01-07-2022 - |
سؤال
I'm working on a project in which I'm using Affine Transform to move round object around a JPanel but I'm not seeing the affect I would aspect.
I'm creating the shape using the Area class with constructive geometry. I set it's location and size as part of the constructor with
// Set location and size
AffineTransform at = new AffineTransform();
at.translate(x, y);
at.scale(size, size);
shape.transform(at);
This appears to work correctly. The issue I see is when I make a separate call to re-size the shape.
/**
* Scales this shape uniformly by the
* amount of scale about the origin
* @param scale - double
*/
public void scaleBy(double scale)
{
double cx = this.getBounds2D().getCenterX();
double cy = this.getBounds2D().getCenterY();
double size = this.getBounds2D().getWidth();
System.out.println("Before ~ Size: " + size + " | Scale: " + scale);
AffineTransform at = new AffineTransform();
at.translate(cx, cy);
at.scale(scale, scale);
at.translate(-cx, -cy);
shape.transform(at);
System.out.println("After ~ Size: " + size + " | Scale: " + scale);
}
The prints statements show that there is no change in size before and after the call:
Before ~ Size: 159.61132183122953 | Scale: 0.9703729137903474
After ~ Size: 159.61132183122953 | Scale: 0.9703729137903474
Before ~ Size: 84.00595885854187 | Scale: 1.0692249107993637
After ~ Size: 84.00595885854187 | Scale: 1.0692249107993637
I guess my question is does the transform not take place until repaint() call is made? If so what would be a good way to restrict the sized to keep the shape between say 30.0 and 150.0 in size
المحلول
لا تنتمي إلى StackOverflow