现在我正在玩JavaFX,我想做的是在已经使用Scene Builder创建的画布上绘制某种动画。不幸的是,我无法在画布上得到任何东西,因为我在使用GraphicsContext做任何事情时都会遇到一些异常。
我对Java和JavaFX都很陌生,我一直在寻找很多东西来解决这个问题,但我无法解决这个问题。
循环似乎已成功运行,因为我能够获取控件打印件,但与gameCanvas
Canvas上的渲染图形相关的任何内容都会触发异常。
错误:(例如,由于gc.setFill(Color.RED)
方法中的run()
,我得到了这些错误。
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at Game.lambda$0(Game.java:54)
at javafx.graphics/com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.shared.TimelineClipCore.playTo(Unknown Source)
at javafx.graphics/javafx.animation.Timeline.doPlayTo(Unknown Source)
at javafx.graphics/javafx.animation.AnimationAccessorImpl.playTo(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(Unknown Source)
at javafx.graphics/javafx.animation.Animation.doTimePulse(Unknown Source)
at javafx.graphics/javafx.animation.Animation$1.lambda$timePulse$0(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/javafx.animation.Animation$1.timePulse(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
我的代码:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import javafx.scene.shape.*;
public class Game implements Initializable, Runnable, ControlledScreen {
@FXML
public Canvas gameCanvas;
private GraphicsContext gc;
public enum difficulties {EASY, MEDIUM, HARD};
difficulties difficultyLevel;
int score = 0;
Snake snake;
Food food;
boolean running = true;
private Timeline gameloop = new Timeline();
@Override
public void setScreenParent(ScreensController screenPage) {
}
public void setLevelEasy() {
difficultyLevel = difficulties.EASY;
}
public void setLevelMedium() {
difficultyLevel = difficulties.MEDIUM;
}
public void setLevelHard() {
difficultyLevel = difficulties.HARD;
}
@Override
public void run() {
KeyFrame frame = new KeyFrame(Duration.seconds(0.2), event -> {
if (!running)
return;
System.out.println("The loop's running, yai!");
gc.setFill(Color.RED);
});
gameloop.getKeyFrames().add(frame);
gameloop.setCycleCount(Timeline.INDEFINITE);
gameloop.play();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
gc = gameCanvas.getGraphicsContext2D();
}
}
提前致谢!