drawImage, AnimationTimer behavior is strange

Asked 2 years ago, Updated 2 years ago, 103 views

There are some problems.

An elapsed time becomes (n-1)/n when drawing is performed once every n times.0.5 seconds for 2 out of 60 frames and 0.66 seconds for 3 frames.
The CPU utilization gradually increases.At first, it was less than 10% and up to 80%.
Each frame drawing abnormally shortens the interval between timers in a hidden state.

This environment is Lubuntu 15.04.

The code is as follows:

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;

public class Main extensions Application {

    int frame;
    long last;

    @ Override
    public void start (Stage primaryStage) threads Exception {
        Group root = new Group();
        primaryStage.setScene(newScene(root));
        Canvas canvas = new Canvas (640,480);
        root.getChildren().add(canvas);
        GraphicsContext gc=canvas.getGraphicsContext2D();
        WriteableImage image = new WriteableImage (32,32);

        new AnimationTimer() {
            @ Override
            public void handle (long now) {
                if(frame%3==0){
                    gc.drawImage(image,0,0);
                }
                if(frame%60==0){
                    primaryStage.setTitle("+(now-last)/1_000_000_000.0);
                    last = now;
                }
                frame++;
            }
        }.start();
        primaryStage.show();
    }

    public static void main(String[]args) {
        launch(args);
    }
}

The result was the same for java1.8.0_102.
When running Windows XP, 7, there was no increase in CPU utilization.
I put Lubuntu in Windows 7 and it went up, so is it a problem on the OS side?

java javafx

2022-09-30 18:13

1 Answers

[This is Windows 10, CPU utilization when executing code is approximately 5% or less.The java ver is 1.8.0_102.
The time interval displayed on the title bar is 0.63 to 0.64.]

The only reason why the interval is shorter to hide is because the image is not painted, so it may be a specification.
The abnormal CPU utilization may be cured by updating java.

コードWe have determined that there is no need to modify the code.]


2022-09-30 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.