How to detect MouseClick on the “stroke” part of a QuadCurve?

女生的网名这么多〃 提交于 2019-12-10 11:06:07

问题


I want to detect mouse click event on a QuadCurve but only on the "stroke" part. To be clear only the green part of the image.

Here, i use a red fill value but in my application i use a transparent fill value. So detecting click on the visible part of the curve is critical.

The sample code :

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.QuadCurve;
import javafx.stage.Stage;

public class QuadCurveMouseHandling extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        QuadCurve curve = new QuadCurve();
        curve.setStartX(10);
        curve.setStartY(10);
        curve.setControlX(50);
        curve.setControlY(250);
        curve.setEndX(300);
        curve.setEndY(300);
        curve.setStyle("-fx-stroke-width: 7;-fx-stroke: green;-fx-fill: red;");
        curve.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                System.out.println("clicked");
            }
        });
        stage.setScene(new Scene(new Group(curve), 320, 320));
        stage.show();
    }

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

回答1:


Set the fill to null rather than a color.

e.g. -fx-fill: null; => if the fill is not there, you can't click on it.




回答2:


The simplest solution is to put the check inside OnMouseClicked handler and check the color of underlying point. If green - run your logic, if not green don't do anything. The more complicated way is to check if underlying point sits on the 'stroke' of your figure running some calculation.



来源:https://stackoverflow.com/questions/18846227/how-to-detect-mouseclick-on-the-stroke-part-of-a-quadcurve

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