correct way to move a node by dragging in javafx 2?

前端 未结 8 803
感动是毒
感动是毒 2020-12-01 05:50

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 pain

相关标签:
8条回答
  • 2020-12-01 05:59

    There's this "cacheHint" property, available on all Nodes and that may help ?

    http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#cacheHintProperty

    Under certain circumstances, such as animating nodes that are very expensive to render, it is desirable to be able to perform transformations on the node without having to regenerate the cached bitmap. An option in such cases is to perform the transforms on the cached bitmap itself.

    This technique can provide a dramatic improvement to animation performance, though may also result in a reduction in visual quality. The cacheHint variable provides a hint to the system about how and when that trade-off (visual quality for animation performance) is acceptable.

    If your ellipse remains the same the whole time, but is redrawn every time you move it by one pixel, this seems to be a huge slowdown.

    0 讨论(0)
  • 2020-12-01 06:03

    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

    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.

    EDIT:

    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.

    0 讨论(0)
  • 2020-12-01 06:07

    I was having the same problem while trying to make nodes on a chart draggable. I fixed it by calling chart.setAnimated(false); In my case the lag was being caused by JavaFX applying a nice smooth animation to the changes my code was making.

    0 讨论(0)
  • 2020-12-01 06:09

    here is the code to drag and drop label using mouse in javafx

    @FXML
    public void lblDragMousePressed(MouseEvent m)
    {
        System.out.println("Mouse is pressed");
    
        prevLblCordX= (int) lblDragTest.getLayoutX();
        prevLblCordY= (int) lblDragTest.getLayoutY();
        prevMouseCordX= (int) m.getX();
        prevMouseCordY= (int) m.getY();     
    }
    //set this method on mouse released event for lblDrag
    @FXML
    public void lblDragMouseReleased(MouseEvent m)
    {       
        System.out.println("Label Dragged");    
    }
    // set this method on Mouse Drag event for lblDrag
    @FXML
    public void lblDragMouseDragged(MouseEvent m)
    {   
        diffX= (int) (m.getX()- prevMouseCordX);
        diffY= (int) (m.getY()-prevMouseCordY );        
        int x = (int) (diffX+lblDragTest.getLayoutX()-rootAnchorPane.getLayoutX());
        int y = (int) (diffY+lblDragTest.getLayoutY()-rootAnchorPane.getLayoutY());     
        if (y > 0 && x > 0 && y < rootAnchorPane.getHeight() && x < rootAnchorPane.getWidth()) 
        { 
         lblDragTest.setLayoutX(x);
         lblDragTest.setLayoutY(y);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 06:10

    To me it doesn't look like a question of painting performance, but how the sequence of mouse events is generated. The events are not generated in real time, some are skipped, when the mouse moves fast. For the most applications this will be the sufficent way. The mouse pointer moves in real time without any time lag.

    If you don't want this effect you will have to listen to the mouse pointer directly or find a way to get the events in higher density. I don't know how myself.

    0 讨论(0)
  • 2020-12-01 06:13

    you can use : Node.setCache(true); (i use it with a Pane with many childrens like a TextField)

    0 讨论(0)
提交回复
热议问题