我正在努力寻找一种方法来选择画布,然后旋转或调整在画布上绘制的几何图形的大小(使用GraphicsContext函数),该功能应该是MS Paint等应用程序的一部分。对于绘制形状,我使用了Canvas的分层系统来允许撤消/重做功能。我最初的想法是使用快照功能来完成此操作,但我不确定如何实现快照以及实现此目的的最佳方法。
更新: 我目前正在处理此代码
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);
}}
以此为起点,有一种方法可以实现: