2

With what reference point does ImageView.setRotate(40) rotates the image in Javafx ?

I am writing

ImageView iv = new ImageView(image);
iv.setRotate(40);
SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT);
Image rotatedImage = iv.snapshot(params, null);
gc.drawImage(rotatedImage, 0, 0);

But the image doesn't rotates with respect to the center of the image.

Tanuj Yadav
  • 1,259
  • 13
  • 21

1 Answers1

1

From the rotate() documentation:

The pivot point about which the rotation occurs is the center of the untransformed layoutBounds.

So, the the image will be rotated about it's center point.

A sample:

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.image.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Rotor extends Application {

    public static final String LOC =
            "http://icons.iconarchive.com/icons/custom-icon-design/flatastic-10/64/Bear-icon.png";

    @Override
    public void start(Stage stage) throws Exception {
        ImageView iv = new ImageView(new Image(LOC));
        iv.setRotate(40);

        SnapshotParameters params = new SnapshotParameters();
        params.setFill(Color.TRANSPARENT);
        Canvas canvas = new Canvas(100, 100);

        Image rotatedImage = iv.snapshot(params, null);
        canvas.getGraphicsContext2D().drawImage(rotatedImage, 0, 0);
        stage.setScene(new Scene(new Group(canvas)));

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

bear

As you are drawing rotated images on a canvas, you might wish to use canvas transforms rather than snapshot methods to help accomplish this. See for example:

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406