JavaFX,使用鼠标选择和移动Canvas图像

时间:2019-03-29 23:04:45

标签: java canvas javafx java-8 mouseevent

我创建了ArrayList并将其绘制在画布上。 Shape类也包含其position和draw方法(使用画布)。我创建了鼠标事件来选择星星并移动。移动时,屏幕会不断刷新。 实际形状不会在画布上移动。对于鼠标事件,我主要引用了Select and Move Canvas image with MouseEvent and MouseClicked in JavaFX这篇帖子。

我检查了形状对象中的坐标肯定在变化,并且我还检查了屏幕是否保持刷新。

public class Drawing1 extends Application {

    public static final double WINDOW_WIDTH = 700;
    public static final double WINDOW_HEIGHT = 400;
    public static final double CANVAS_HEIGHT = 300;

    GraphicsContext gc;
    ArrayList<Star> stars = new ArrayList<Star>();
    double startX;
    double startY;
    Color stroke, fill;

    private void clear() {
        gc.setFill(Color.WHITE);
        gc.fillRect(0, 0, WINDOW_WIDTH, CANVAS_HEIGHT);
    }

    private void redraw() {
        clear();
        for (Star star : stars) {
            star.draw();
        }
        System.out.println("refresh");
    }

    @Override
    public void start(Stage stage) throws Exception {
        GridPane root = new GridPane();
        Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
        stage.setTitle("Drawing");
        stage.setScene(scene);

        // *** Canvas
        Canvas canvas = new Canvas(WINDOW_WIDTH, CANVAS_HEIGHT);
        root.add(canvas, 0, 0, 3, 1);
        gc = canvas.getGraphicsContext2D();
        clear();

        // *** (trying) drag
        stars.add(new Star(gc, 100, 100, 100, 100, Color.BLACK, Color.AQUA));
        stars.add(new Star(gc, 150, 150, 100, 100, Color.BLACK, Color.YELLOW));

        canvas.setOnMouseClicked(e -> this.select(e));
        canvas.setOnMouseMoved(e -> {
            if (this.nowSelected) {
                this.move(e, canvas);
            }
        });

        stage.show();
    }

    double mouse_x = 0.0;
    double mouse_y = 0.0;
    Star selectedStar;
    boolean nowSelected;

    public void select(MouseEvent e) {
        double temp_mouse_x = e.getSceneX();
        double temp_mouse_y = e.getSceneY();

        for (int i = stars.size() - 1; i >= 0; i--) {
            Star star = stars.get(i);
            double x_max = star.getX() + star.getWidth();
            double y_max = star.getY() + star.getHeight();
            nowSelected = temp_mouse_x >= star.getX() && temp_mouse_x <= x_max // x-area
                    && temp_mouse_y >= star.getY() && temp_mouse_y <= y_max; //y-area              

            this.mouse_x = temp_mouse_x;
            this.mouse_y = temp_mouse_y;

            if (!nowSelected) {
                star.setSelected(false);
            } else if (star.isSelected() && nowSelected) { // deselect the circle if already selected
                star.setSelected(false);
                System.out.println(i + " index star deselected");
            } else {
                star.setSelected(true);
                System.out.println(i + " index star selected");
                selectedStar = star;   // needs deselector for all the others
                return;
            }
        }
    }

    public void move(MouseEvent e, Canvas canvas) {
        selectedStar.setX(this.mouse_x);
        selectedStar.setY(this.mouse_y);
        redraw();
        this.mouse_x = e.getSceneX();
        this.mouse_y = e.getSceneY();
        System.out.println(selectedStar);
    }

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

我希望选定的形状可以用鼠标移动。 顺便说一句,它实际上选择包含的正方形区域,而不是实际区域。有没有一种方法可以准确地选择在画布上绘制的图像,而不是使用Shape扩展类?

0 个答案:

没有答案