Resize or Rotate images on JavaFX canvas

最后都变了- 提交于 2019-12-25 00:33:40

问题


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

来源:https://stackoverflow.com/questions/55222189/resize-or-rotate-images-on-javafx-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!