通过在javafx 2中拖动来移动节点的正确方法? [英] correct way to move a node by dragging in javafx 2?

查看:195
本文介绍了通过在javafx 2中拖动来移动节点的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将带有大量自定义绘画的Swing / Graphics2D应用程序转换为JavaFX2应用程序。虽然我非常喜欢新的API,但是在绘制一个椭圆时我似乎有一个性能问题,我想在鼠标移动的任何地方绘制鼠标光标。当我以稳定的方式移动我的鼠标,而不是快速的滑动时,我注意到椭圆总是在鼠标轨迹后面几厘米处被绘制,并且只有在我停止移动光标时才会捕获。这在一个只有极少数节点的场景图中。在我的Swing应用程序中,我没有遇到这个问题。

I'm converting a Swing/Graphics2D app with a lot of custom painting to a JavaFX2 app. Although I absolutely love the new API, I seem to have a performance problem when painting an ellipse that I want to paint below the mouse cursor wherever the mouse is moved. When I move my mouse in a steady way, not ridicously fast, I notice the ellipse is always drawn a few centimeters behind on the mouse trail, and only catches up when I stop moving the cursor. This in a scenegraph with only a handful nodes. In my Swing app I didn't have that problem.

我想知道这是否是绘制鼠标光标所在的形状的正确方法?

I'm wondering if this is the correct approach for drawing a shape where the mousecursor is?

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.EllipseBuilder;
import javafx.stage.Stage;

public class TestApp extends Application {
public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
    Pane p = new Pane();

    final Ellipse ellipse = EllipseBuilder.create().radiusX(10).radiusY(10).fill(Color.RED).build();
    p.getChildren().add(ellipse);

    p.setOnMouseMoved(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            ellipse.setCenterX(event.getX());
            ellipse.setCenterY(event.getY());
        }
    });

    Scene scene = SceneBuilder.create().root(p).width(1024d).height(768d).build();
    primaryStage.setScene(scene);

    primaryStage.show();
}
}

小更新:我虽然升级到JavaFX 2.2和Java7u6(在Windows 7 64bit上),但似乎没有什么区别。

Small update: I upgraded to JavaFX 2.2 and Java7u6 (on Windows 7 64bit), doesn't seem to make a difference though.

推荐答案

滞后您所描述的(在鼠标和拖动的形状之间)是一个已知的JavaFX错误:

The lag that you're describing (between your mouse and the dragged shape) is a known JavaFX bug:

https://bugs.openjdk.java.net/browse/JDK-8087922

您可以使用未记录的JVM标志解决它(至少在Windows上):

You can work around it (on Windows, at least) by using an undocumented JVM flag:

-Djavafx.animation.fullspeed=true

此标志通常用于内部性能测试,这就是为什么它没有文档,但我们已经已经使用了几个月,到目前为止还没有任何问题。

This flag is normally for internal performance testing, which is why it is undocumented, but we've been using it for months and haven't had any problems with it so far.

编辑:

这里有另一种类似的方法来解决这个可能在CPU使用上更容易的错误。只需关闭Prism的垂直同步:

There's another, similar way to workaround this bug that might be a little easier on CPU usage. Simply turn off Prism's vertical sync:

-Dprism.vsync=false

在我们的应用程序中,这些变通方法中的任何一个都可以解决滞后问题。没有必要同时做这两件事。

In our app, either of these workarounds solves the lag; there's no need to do both.

这篇关于通过在javafx 2中拖动来移动节点的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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