如何从列表中选择特定节点并对其应用效果? [英] How to select specific node from list and apply effect to it?

查看:133
本文介绍了如何从列表中选择特定节点并对其应用效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ArrayList,里面有Path对象,每个路径都有自己的EventHandler。但是,当我将鼠标悬停在某个路径上时,效果.setOnMouseEntered将应用于ArrayList中的所有路径而不是特定路径:

I have an ArrayList that holds Path objects inside, and each one of those paths has it's own EventHandler. However when i mouse over a certain path then effect .setOnMouseEntered applies to all the paths inside the ArrayList and not the certain one:

Path path;
Group root = new Group();
ArrayList<Path> paths = new ArrayList<Path>();
EventHandler<MouseEvent> mouseEventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle (MouseEvent e) {
    if (i == 0) {
    if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
        path = new Path();
        path.setStroke(Color.BLACK);
        path.setStrokeWidth(10);
        root.getChildren().add(path);
        path.getElements().add(new MoveTo(e.getX(), e.getY()));
    }
    if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        path.getElements().add(new LineTo(e.getX(), e.getY()));
    }
    if (e.getEventType() == MouseEvent.MOUSE_RELEASED) {
        path.getElements().add(new LineTo(e.getX(), e.getY()));
        for (Path path : paths) {
        path.setOnMouseEntered(mouseEventHandler2);
        path.setOnMouseExited(mouseEventHandler2);
        path.setOnMouseDragged(mouseEventHandler2);
        }
        paths.add(path);
    }
}
}
EventHandler<MouseEvent> mouseEventHandler2 = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
    if (me.getEventType() == MouseEvent.MOUSE_ENTERED) {
        path.setEffect(new DropShadow(20, Color.BLACK));
    }
    if (me.getEventType() == MouseEvent.MOUSE_EXITED) {
        path.setEffect(null);
    }
    if (me.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        path.relocate(me.getX(), me.getY());
    }
}
};

所以这里mouseEventHandler2应用于ArrayList路径中的所有路径,而不是某个具有MOUSE_ENTERED的路径MOUSE_EXITED

So here mouseEventHandler2 is applied to all the Paths inside ArrayList paths, not that certain one which has MOUSE_ENTERED or MOUSE_EXITED

推荐答案

因为每次点击都要创建一个新的路径并且在场景上拖动,没有必要遍历整个路径列表来分配事件处理程序。只需将它们添加到最后一个新路径即可。

Since you are creating a new Path every time you click and drag on the scene, it is not necessary to go over the whole list of paths to assign the event handlers. Just add them to the last new path.

此外,还有另一个问题,因为你在路径 > mouseEventHandler2 。您应该参考 e.getSource()中的路径。否则,只有最后一个路径会对鼠标事件做出反应。

Besides, there's another problem, since you're using the global variable path on the mouseEventHandler2. You should refer to the path from e.getSource(). Otherwise, only the last path will react to mouse events.

最后一个问题:如果你试图拖动现有路径,同时你要创建一个新路径...我猜这是 i == 0 部分,这对我没有意义(假设 i 是路径的数量,我不明白为什么你只限制为1然后使用for循环......)。

One last problem: if you try to drag an existing path, at the same time you are creating a new one... I guess that's the i==0 part, which doesn't make sense to me (assuming i is the number of paths, I don't see why you limit to just to 1 and then use a for-loop...).

基本上,这些是我对你的代码的修改(第二个拖动事件处理程序被注释,并且对路径创建没有限制)。

Basically, these are my modifications to your code (with the second drag event handler commented, and with no limit to path creation).

private final EventHandler<MouseEvent> mouseEventHandler2 = (MouseEvent me) -> {
    Path path1 = (Path)me.getSource();
    if (me.getEventType() == MouseEvent.MOUSE_ENTERED) {
        path1.setEffect(new DropShadow(20, Color.BLACK));
    }
    if (me.getEventType() == MouseEvent.MOUSE_EXITED) {
        path1.setEffect(null);
    }
        if (me.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            path1.relocate(me.getX(), me.getY());
        }
};

private final EventHandler<MouseEvent> mouseEventHandler = (MouseEvent e) -> {
    if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
        path = new Path();
        path.setStroke(Color.BLACK);
        path.setStrokeWidth(10);
        path.getElements().add(new MoveTo(e.getX(), e.getY()));
        root.getChildren().add(path);
    }
    if (e.getEventType() == MouseEvent.MOUSE_DRAGGED) {
        path.getElements().add(new LineTo(e.getX(), e.getY()));
    }
    if (e.getEventType() == MouseEvent.MOUSE_RELEASED) {
        path.getElements().add(new LineTo(e.getX(), e.getY()));
        path.setOnMouseEntered(mouseEventHandler2);
        path.setOnMouseExited(mouseEventHandler2);
//        path.setOnMouseDragged(mouseEventHandler2);
    }
};

这篇关于如何从列表中选择特定节点并对其应用效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆