gpt4 book ai didi

Javafx:用鼠标在ImageView上绘图

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:29:21 24 4
gpt4 key购买 nike

用鼠标在 ImageView 上绘图的任何方法..我不知道,例如使用 Canvas ?谢谢

最佳答案

我发现这是解决我的问题的一个很好的方法..不是吗?

    AnchorPane anchorRoot;
double initX;
double initY;
ImageView iv = new ImageView(image);
final double maxX = iv.getImage().getWidth();
final double maxY = iv.getImage().getHeight();

anchorRoot.getChildren().add(iv);

iv.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
//System.out.println("Clicked, x:" + me.getSceneX() + " y:" + me.getSceneY());
//the event will be passed only to the circle which is on front
initX = me.getSceneX();
initY = me.getSceneY();
me.consume();
}
});
iv.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
//System.out.println("Dragged, x:" + me.getSceneX() + " y:" + me.getSceneY());
if (me.getSceneX() < maxX && me.getSceneY() < maxY) {
Line line = new Line(initX, initY, me.getSceneX(), me.getSceneY());
line.setFill(null);
line.setStroke(Color.RED);
line.setStrokeWidth(2);
anchorRoot.getChildren().add(line);
}

initX = me.getSceneX() > maxX ? maxX : me.getSceneX();
initY = me.getSceneY() > maxY ? maxY : me.getSceneY();
}
});

关于Javafx:用鼠标在ImageView上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14755968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com