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

查看:27
本文介绍了通过在 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 64 位上),但似乎没有什么区别.

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天全站免登陆