0

I'm struggling find a way to select and then rotate or resize geometric figures drawn on a canvas (using the GraphicsContext function), this funciont should be part of an application like MS Paint. For drawing shapes I'm using a layered system of Canvas to allow Undo/Redo function. My original idea was to use the snapshot function to accomplish this operation, but I'm not really sure of how implement it and if the best method to do it.

UPDATE: I'm currently working on this code

public class Sandbox extends Application {

@Override
public void start(Stage primaryStage) {

    AnchorPane pane = new AnchorPane();

    Scene scene = new Scene(pane, 600, 400);

    Canvas canvas = new Canvas(600, 400);
    GraphicsContext gc = canvas.getGraphicsContext2D();

    gc.strokeOval(20, 20, 100, 150);
    gc.setStroke(Color.BLACK);
    pane.getChildren().add(canvas);

    primaryStage.setTitle("Prova");
    primaryStage.setScene(scene);
    primaryStage.show();

    ImageView imageView = new ImageView();

    canvas.setOnMousePressed(event -> {
        int x1 = (int) event.getX();
        int y1 = (int) event.getX();


        canvas.setOnMouseReleased(e -> {

            int x2 = (int) e.getX();
            int y2 = (int) e.getY();

            Rectangle2D rect = new Rectangle2D(x1, y1, x2, y2);

            SnapshotParameters params = new SnapshotParameters();
            params.setViewport(rect);
            params.setTransform(new Scale(0.5, 0.5));
            WritableImage write = new WritableImage(x1 + x2, y1 + y2);
            Image image = canvas.snapshot(params, write);
            imageView.setImage(image);
            gc.clearRect(x1, y1, x2, y2);
            gc.drawImage(image, x1, y1);
        });
    });

}

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

From this as startpoint there is a way to:

  1. Make the selection without execute immediatly the transformation
  2. A way to highlight the selection
tenser
  • 1
  • 3
  • Does [this](https://stackoverflow.com/questions/39104251/rotate-object-around-center-in-javafx) answer help? – Edgar H Mar 18 '19 at 13:29
  • Thank you for the comment, but this answer is not applicable to my case, I have 2D shapes drawn on various layer, what I'm trying to do is select one or more shapes and then resize or rotate them. – tenser Mar 18 '19 at 13:52

0 Answers0