Reputation: 63
i have a problem with the scaling ability for my shapes. I want to use a coordinate system so drawn by my own which holds some shapes. These shapes needs a scaling function. After scaling the shapes they would move because the scaling takes place at the center of each shape. To react on this movement i recalculate the origin. But if i scale many times the shape still move away from origin. i dont know why. Can anyone explain this behavior? Further i try to recalculate the stroke width to stay 1px.
Thank you!
My code:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polyline;
import javafx.stage.Stage;
public class ScaleTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 800, 600, Color.WHITE);
stage.setScene(scene);
Polyline line1 = new Polyline(new double[] { 50, 50, 50, 150, 150, 150,
150, 50, 50, 50 });
line1.setStrokeWidth(1);
Group polylines = new Group();
polylines.getChildren().add(line1);
Line xAxis = new Line(0, 50, 800, 50);
Line yAxis = new Line(50, 0, 50, 800);
root.setScaleY(-1);
root.getChildren().addAll(xAxis, yAxis);
root.getChildren().add(polylines);
scene.setOnScroll(new EventHandler<ScrollEvent>() {
@Override
public void handle(ScrollEvent event) {
double scale = 0;
if (event.getDeltaY() < 0) {
scale = -0.1;
} else {
scale = 0.1;
}
Bounds beforeScaling = polylines.getBoundsInParent();
polylines.setScaleX(polylines.getScaleX() + scale);
polylines.setScaleY(polylines.getScaleY() + scale);
Bounds afterScaling = polylines.getBoundsInParent();
polylines.setTranslateX(polylines.getTranslateX()
+ beforeScaling.getMinX() - afterScaling.getMinX());
polylines.setTranslateY(polylines.getTranslateY()
+ beforeScaling.getMinY() - afterScaling.getMinY());
//optional
line1.setStrokeWidth(1/polylines.getScaleX());
}
});
stage.show();
}
}
Upvotes: 5
Views: 4009
Reputation: 209358
I'm not sure why it is "drifting" - possibly it is just accumulating rounding errors. An easier approach anyway is to use a Scale
transformation. With this you can specify the "pivot" point about which the scale occurs.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polyline;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
public class ScaleTest extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 800, 600, Color.WHITE);
stage.setScene(scene);
Polyline line1 = new Polyline(new double[] { 50, 50, 50, 150, 150, 150,
150, 50, 50, 50 });
line1.setStrokeWidth(1);
Group polylines = new Group();
polylines.getChildren().add(line1);
Line xAxis = new Line(0, 50, 800, 50);
Line yAxis = new Line(50, 0, 50, 800);
root.setScaleY(-1);
root.getChildren().addAll(xAxis, yAxis);
root.getChildren().add(polylines);
Scale scale = new Scale();
scale.setPivotX(50);
scale.setPivotY(50);
polylines.getTransforms().add(scale);
scene.setOnScroll(new EventHandler<ScrollEvent>() {
@Override
public void handle(ScrollEvent event) {
double scaleDelta = 0;
if (event.getDeltaY() < 0) {
scaleDelta = -0.1;
} else {
scaleDelta = 0.1;
}
scale.setX(scale.getX() + scaleDelta);
scale.setY(scale.getY() + scaleDelta);
//optional
line1.setStrokeWidth(1/scale.getX());
}
});
stage.show();
}
}
Upvotes: 7