I was messing around with the JavaFX API, and for some reason this application seems to freeze after a (seemingly) random amount of time.
It is an application that makes a red-green gradient pattern, and has a kinda cool animation to go along with it. When the application is run, press the Enter key, and the animation will start. After some amount of seconds (seemingly random as I said before) it will stop updating, but the timer continues to run, and so does the loop, and the setColor method is still being called with the proper arguments, leading me to think that either the PixelWriter is frozen or the window isn't updating.
The code that I have done 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, y, 0) : Color.WHITE);
}
}
}
column++;
if(column >= CoolAnimation.this.getWidth() * 2) {
coloring ^= true;
column = 0;
}
}
}
}, 0L, 10L);
}
}
}
Huge thanks to anyone who is able to help!