在javafx中绘制一个多边形

时间:2017-07-19 21:14:52

标签: java javafx javafx-8

我正在尝试在javafx中绘制一个多边形,添加到鼠标坐标点的数组。我的问题是,当我点击鼠标左键单击其他内容时,我不知道如何停止它。

setInterval()

2 个答案:

答案 0 :(得分:3)

您可以创建对多边形的引用。如果是第一次单击,则多边形将为null,因此请创建一个新的并将其添加到窗格中。然后继续添加点,直到右键单击,此时您只需将多边形设置回null,以便下一次左键单击再次开始一个新的多边形。

SSCCE:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;

public class DrawPolygon extends Application {

    private Polygon currentPolygon ;

    @Override
    public void start(Stage primaryStage) {
        Pane rootPane = new Pane();
        rootPane.setMinSize(600, 600);
        rootPane.setOnMouseClicked(e -> {
            if (e.getButton() == MouseButton.PRIMARY) {
                if (currentPolygon == null) {
                    currentPolygon = new Polygon();
                    currentPolygon.setStroke(Color.BLACK);
                    rootPane.getChildren().add(currentPolygon);
                }
                currentPolygon.getPoints().addAll(e.getX(), e.getY());
            } else {
                currentPolygon = null ;
            }
        });
        Scene scene = new Scene(rootPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

您可以围绕此方式玩各种想法,以获得不同的用户体验,例如

    rootPane.setOnMouseClicked(e -> {
        if (e.getButton() == MouseButton.PRIMARY) {
            if (currentPolygon == null) {
                currentPolygon = new Polygon();
                currentPolygon.getPoints().addAll(e.getX(), e.getY());
                currentPolygon.setStroke(Color.BLACK);
                rootPane.getChildren().add(currentPolygon);
            }
            currentPolygon.getPoints().addAll(e.getX(), e.getY());
        } else {
            currentPolygon = null ;
        }
    });
    rootPane.setOnMouseMoved(e -> {
        if (currentPolygon != null) {
            currentPolygon.getPoints().set(currentPolygon.getPoints().size()-2, e.getX());
            currentPolygon.getPoints().set(currentPolygon.getPoints().size()-1, e.getY());
        }
    });

答案 1 :(得分:1)

问题是您的代码卡在一个单独的事件中。即使您移动鼠标或释放鼠标按钮,您正在使用的事件实例的值也不会改变。

将事件视为单一状态。当发生重要事件时(在您的情况下,单击鼠标按钮),javafx将使用MouseEvent实例调用mouseEventHandler,并在该时刻使用鼠标状态。当您再次单击时,javafx将使用新值创建一个新实例,并再次调用eventHandler。

为了使这项工作,您需要一个不同的鼠标事件(或稍微修改它,以便它只在单击鼠标时设置一个点)。你需要丢失(无限)while循环,因为它既阻止了EventThread,也没有为你需要它做的事情工作。所以这样的事情可能会好一点。

// this will add a point for every (secondary)mousebutton click
rootPane.setOnMouseClicked((MouseEvent me) -> {
    if(me.isSecondaryButtonDown())
        polygon.getPoints().addAll(me.getX(),me.getY());
     });

// this will add a point for every mousemovement while the secondary mousebutton is down.
rootPane.setOnMouseMoved((MouseEvent) me -> {
    if(me.isSecondaryButtonDown())
        polygon.getPoints().addAll(me.getX(),me.getY());
});

现在有一个MouseDragEvent,但这主要用于移动数据(如图像和文件),但我不推荐它。在你的情况下,它不是很有用,它的行为仍然是有问题的。

我希望这能帮助你朝着正确的方向前进。

相关问题