JavaFX freeze problem

I'm messing with the JavaFX API. For some reason, the application seems to freeze after a (seemingly) random time

It is an application that makes red - green gradient patterns and has a cool animation to use with it When you run the application, press enter and the animation will begin After a period of time (it looks as random as I said before), it stops updating, but the timer continues to run, so does the loop, and still calls the SetColor method with the correct parameters, which makes me think that the pixelwriter is frozen or the window is not updated

My code is as follows:

package me.dean;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.PixelWriter;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.Timer;
import java.util.TimerTask;
public class DeansApp extends Application {
    CoolAnimation canvas;

    @Override
    public void start(Stage primaryStage) throws Exception {
        canvas = new CoolAnimation();
        canvas.setWidth(255);
        canvas.setHeight(255);
        primaryStage.setScene(new Scene(new Pane(canvas)));
        canvas.getScene().setOnKeyPressed(event -> {
            if(event.getCode().equals(KeyCode.ENTER)) {
                canvas.play ^= true;
            }
        });

        primaryStage.setOnCloseRequest(event -> System.exit(0));
        primaryStage.show();
    }

    private class CoolAnimation extends Canvas {
        boolean play;
        int column = 0;
        boolean coloring = true;

        public CoolAnimation() {
            super();

            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    if(CoolAnimation.this.play) {
                        GraphicsContext g = getGraphicsContext2D();
                        if(g != null) {
                            PixelWriter writer = g.getPixelWriter();
                            if(writer != null) {
                                for (int x = Math.min(255,column),y = Math.max(0,column - (int) CoolAnimation.this.getWidth());
                                     x >= 0 && y<=255;
                                     x--,y++) {
                                    writer.setColor(x,y,coloring ? Color.rgb(x,0) : Color.WHITE);
                                }
                            }
                        }
                        column++;
                        if(column >= CoolAnimation.this.getWidth() * 2) {
                            coloring ^= true;
                            column = 0;
                        }
                    }
                }
            },0L,10L);
        }
    }
}

Thank you very much to anyone who can help!

Solution

You want to use animationtimer or timeline instead of Java util. Timer. Animaltimer or timeline executes its code on the JavaFX application thread, while Java util. Timer does not execute Using Java util. Timer, you will encounter random problems related to thread contention conditions You can connect the platform Runlater and Java util. Timers are used in combination, but animationtimer is usually the preferred method to deal with this problem

View related questions:

> javafx animation looping

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>