下面是一个简单的示例,使用JavaFX实现了一个基本的图形编辑器,可以绘制矩形和圆形。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class GraphicsEditor extends Application {
private Canvas canvas;
private GraphicsContext gc;
private ShapeType currentShapeType;
private double startX, startY, endX, endY;
public enum ShapeType {
RECTANGLE, CIRCLE
}
@Override
public void start(Stage primaryStage) throws Exception {
canvas = new Canvas(800, 600);
gc = canvas.getGraphicsContext2D();
canvas.setOnMousePressed(e -> {
startX = e.getX();
startY = e.getY();
});
canvas.setOnMouseReleased(e -> {
endX = e.getX();
endY =