所以我试图使用JavaFX。我尝试使用BPF和JavaFX进行动画处理。在这个节目中,我试图让地球围绕太阳运动。它在运行之前没有给出任何错误,但是当我运行它时会遇到这个错误。可能会发生这种情况,因为我可能没有正确导入图像,但我对此非常有信心。
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.Image;
import javafx.stage.Stage;
public class Animation extends Application {
public int canvasSize = 512; // constants for relevant sizes
public double orbitSize = canvasSize / 3;
public double sunSize = 80;
public double earthSize = 30;
public GraphicsContext gc;
public Image earth = new Image(getClass().getResourceAsStream("earth.png"));
public Image sun = new Image(getClass().getResourceAsStream("sun.png"));
// note loading of images, which should be in package
/**
* drawIt ... draws object defined by given image at position and size
* @param i
* @param x
* @param y
* @param sz
*/
public void drawIt (Image i, double x, double y, double sz) {
gc.drawImage(i, x - sz/2, y - sz/2, sz, sz );
}
/**
* draw system, with Earth at x,y
* @param x
* @param y
*/
private void drawSystem(double x, double y) {
// now clear canvas and draw sun and moon
gc.clearRect(0, 0, canvasSize, canvasSize); // clear canvas
drawIt( sun, canvasSize/2, canvasSize/2, sunSize ); // draw Sun
drawIt( earth, x, y, earthSize ); // draw Earth
}
/**
* draw system with Earth at specified angle
* @param t
*/
private void drawSystem(double t) {
double x = canvasSize/2 + orbitSize * Math.cos(t); // calc x coord
double y = canvasSize/2 + orbitSize * Math.sin(t); // and y
drawSystem(x,y); // and draw system
}
/**
* main function ... sets up canvas and timer
*/
@Override
public void start(Stage stagePrimary) throws Exception {
stagePrimary.setTitle("Solar System"); // set title
Group root = new Group(); // create group of what is to be shown
Canvas canvas = new Canvas( canvasSize, canvasSize );
// create canvas to draw on
root.getChildren().add( canvas ); // add canvas to root
gc = canvas.getGraphicsContext2D();
// remember context of canvas drawn on
Scene scene = new Scene( root ); // put root in a scene
stagePrimary.setScene( scene ); // apply the scene to the stage
final long startNanoTime = System.nanoTime();
// for animation, note start time
new AnimationTimer() // create timer
{
public void handle(long currentNanoTime) {
// define handle for what do at this time
double t = (currentNanoTime - startNanoTime) / 1000000000.0;
// calculate time
drawSystem(t); // draw system with Earth at this time
}
}.start(); // start timer
stagePrimary.show(); // show scene
}
public static void main(String[] args) {
Application.launch(args); // launch the GUI
}
}
以下是代码:
{{1}}
提前致谢。