I created an animation of several text nodes. It is assumed that the user should read the text as it receives it from the server. The problem is that after a few minutes (about 5), performance starts to decline. 60 to 30 frames per second and below. As a result, the text is very difficult to read.
Edit 2:
I created an example of Minimal, Complete and Verifiable:
There are 3 files in the project:
MainFxApp:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.util.List;
public class MainFxApp extends Application {
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
root.setStyle("-fx-background-color: black;");
MyAnimationTimer myAnimationTimer = new MyAnimationTimer((List<MyText>) (List<?>) root.getChildren());
MyText newText;
for (int i = 65; i < 85; i++) {
newText = new MyText("" + ((char) i));
newText.setFill(Color.GREEN);
newText.setFont(Font.font(40));
myAnimationTimer.addNode(newText);
}
Scene scene = new Scene(root, 1200, 600);
primaryStage.setTitle("Performance test");
primaryStage.setScene(scene);
primaryStage.show();
myAnimationTimer.start();
}
public static void main(String[] args) {
launch(args);
}
}
MyAnimationTimer:
import javafx.animation.AnimationTimer;
import javafx.application.Platform;
import javafx.scene.CacheHint;
import javafx.scene.Node;
import java.util.List;
public class MyAnimationTimer extends AnimationTimer {
private List<MyText> nodes;
private double panelWidth;
private double panelHeight;
private double basicVelocity;
private long lastFrameTime;
private long timeCount = 0;
private int frameCount = 0;
MyAnimationTimer(List<MyText> nodes) {
super();
this.nodes = nodes;
this.panelWidth = 1200;
this.panelHeight = 600;
this.setBasicVelocity();
}
@Override
public void start() {
this.lastFrameTime = System.nanoTime();
super.start();
}
@Override
public void handle(long now) {
long deltaT = now - lastFrameTime;
double deltaX = this.basicVelocity * deltaT;
for (MyText node : this.nodes) {
node.setTranslateX(node.getTranslateX() + node.direction * deltaX);
if (node.getTranslateX() < 0) {
node.direction = 1;
} else if (node.getTranslateX() > 1200) {
node.direction = -1;
}
}
this.lastFrameTime = now;
this.timeCount += deltaT;
this.frameCount++;
if (timeCount > 1000000000) {
System.out.println(this.frameCount / (double) timeCount * 1000000000);
this.frameCount = 0;
this.timeCount = 0;
}
}
void addNode(final MyText node) {
Platform.runLater(() -> {
node.setCache(true);
node.setCacheHint(CacheHint.SPEED);
node.setTranslateY(panelHeight / 2);
double nodePositionX = panelWidth - 20;
if (nodes.size() >= 1) {
Node lastNode = nodes.get(nodes.size() - 1);
double lastNodeEnd = lastNode.getTranslateX() + 50;
if (lastNodeEnd > nodePositionX) {
nodePositionX = lastNodeEnd;
}
}
node.setTranslateX(nodePositionX);
nodes.add(node);
});
}
private void setBasicVelocity() {
Platform.runLater(() -> {
this.basicVelocity = ((panelWidth / 4) * 3 / (double) 5000 / 1000000.0);
});
}
}
MyText:
import javafx.scene.text.Font;
import javafx.scene.text.Text;
class MyText extends Text {
int direction = -1;
MyText(String text) {
super(text);
this.setFont(new Font("Arial Regular", 40));
}
}
Even with such a simple example, the performance drop is significant. There are 20 nodes on the scene, and fps drops below 20. My processor is the i5-4440 CPU (3.10GHz × 4). The problem occurs on every platform on which I tested it - JavaFX 8, JavaFX 9, and Ubuntu 16.04.
Edit 3:
The problem seems to be present only on the Linux platform.
Windows, JavaFX Platform.runLater, , 60 . - , ?
,