Ahmed Elshorbagy
Ahmed Elshorbagy

Reputation: 611

rotate javafx image view around custom point

I am starting to learn JavaFX now when I try to rotate the image view using

imgv.setRotate(angle);

it rotates around its central axis but when I try to rotate it around a custom point inside the image using

imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y);

it rotates randomly and I can't figure its axis of the rotation this was the same as

 imgv.getTransforms().add(new Rotate(Angle,custom_x,custom_y,1.0,Rotate.Z_AXIS);

is there any way to rotate the image around a custom point here is image explaining the position of a point if (0,0) was at the top left or at the center of the image both way I can't rotate the image around that point according to x,y I know that I can rotate around the origin then do translation but am asking if there is a speedway to do that thanks in advance enter image description here

Upvotes: 1

Views: 2732

Answers (1)

SedJ601
SedJ601

Reputation: 13858

Here is an app that demonstrates how to accomplish what you are asking. The key parts of accomplishing this are .getTransforms().add(..) and Rotate. Comments in the code. I added the Circle so that you can actually see where the point of rotation is located.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication117 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        Image image = new Image("http://lmsotfy.com/so.png");

        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(400);
        imageView.setFitWidth(400);
        Button btn = new Button();
        btn.setText("Say 'Hello World'");

        //Use this Circle to help see where the rotation occurs
        Circle circle = new Circle(5);
        circle.setFill(Color.RED);
        circle.setCenterX(100);
        circle.setCenterY(300);

        //Add the Rotate to the ImageView's Transforms
        Rotate rotation = new Rotate();
        rotation.setPivotX(circle.getCenterX());//Set the Pivot's X to be the same location as the Circle's X. This is only used to help you see the Pivot's point
        rotation.setPivotY(circle.getCenterY());//Set the Pivot's Y to be the same location as the Circle's Y. This is only used to help you see the Pivot's point
        imageView.getTransforms().add(rotation);//Add the Rotate to the ImageView

        //Use the Button's handler to rotate the ImageView
        btn.setOnAction((ActionEvent event) -> {
            rotation.setAngle(rotation.getAngle() + 15);
        });

        Pane pane = new Pane();
        pane.getChildren().addAll(imageView, circle);
        VBox.setVgrow(pane, Priority.ALWAYS);

        VBox vBox = new VBox(pane, new StackPane(btn));

        StackPane root = new StackPane();
        root.getChildren().add(vBox);

        Scene scene = new Scene(root, 1080, 720);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

Upvotes: 2

Related Questions