Test of JavaFX


[Up]
Launch the application via Java Web Start.

Source Code

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.BorderPane;

public class FBall extends Application implements Runnable {
    int x = 20, y = 300, r = 7;
    int vx = 5, vy = 0, a = 3;
    double d = 0.88;
    Thread thr;
    Circle c;
    
    public static void main(String args[]) {
	launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
	BorderPane bd = new BorderPane();
	Pane root = new Pane();
        Scene scene = new Scene(bd, 400, 400, Color.WHITE);

	Button b = new Button("Quit");
	b.setOnAction((ActionEvent) -> {
		System.exit(0);
	    });

	bd.setTop(b);
	bd.setCenter(root);
        stage.setTitle("FBall app");
	stage.setScene(scene);
        stage.show();

	draw(root);
	if (thr == null) {
	    thr = new Thread(this);
	    thr.start();
	}
    }

    void draw(Pane root) {
	c = new Circle(50+x-r, 380-y+r, r);
	c.setFill(Color.BLUE);
	root.getChildren().add(c);
    }

    public void run() {
	while(true) {
	    x += vx; y -= vy;
	    vy += a;
	    if (x > 300) {
		x = 600-x; vx *= -1;
	    }
	    else if (x < 0) {
		x *= -1; vx *= -1;
	    }
	    if (y < 0) {
		y = (int)(-(double)y * d); vy = (int) (-(double) vy * d);
	    }
	    else if (y == 0 && vy == 0) {
		x = 20; y = 300;
	    }
	    c.setCenterX(50+x-r);
	    c.setCenterY(380-y+r);
	    
	    try{
	    	Thread.sleep(50);
	    }
	    catch(InterruptedException e) { }
	}
    }
}

naniwa@rbt.his.u-fukui.ac.jp