Skip to content
Snippets Groups Projects
App.java 1.88 KiB
Newer Older
Jan Kožusznik's avatar
Jan Kožusznik committed
package lab;

Jan Kožusznik's avatar
Jan Kožusznik committed
import javafx.animation.AnimationTimer;
Jan Kožusznik's avatar
Jan Kožusznik committed
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 *  Class <b>App</b> - extends class Application and it is an entry point of the program
 * @author     Java I
 */
public class App extends Application {

	public static void main(String[] args) {
		launch(args);
	}
	
	private Canvas canvas;
Jan Kožusznik's avatar
Jan Kožusznik committed
	private AnimationTimer animationTimer;
Jan Kožusznik's avatar
Jan Kožusznik committed
	
	@Override
	public void start(Stage primaryStage) {
		try {
			//Construct a main window with a canvas.  
			Group root = new Group();
			canvas = new Canvas(800, 400);
			root.getChildren().add(canvas);
			Scene scene = new Scene(root, 800, 400);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.resizableProperty().set(false);
Jan Kožusznik's avatar
Jan Kožusznik committed
			primaryStage.setTitle("Java 1 - 2nd laboratory");
Jan Kožusznik's avatar
Jan Kožusznik committed
			primaryStage.show();
			
Jan Kožusznik's avatar
Jan Kožusznik committed
			
Jan Kožusznik's avatar
Jan Kožusznik committed
			
			//Draw scene on a separate thread to avoid blocking UI.
Jan Kožusznik's avatar
Jan Kožusznik committed
			animationTimer = new AnimationTimer() {
				private Long previous;
				
				@Override
				public void handle(long now) {
					if (previous == null) {
						previous = now;
					} else {
						drawScene((now - previous)/1e9);
						previous = now;
					}
				}
			};
			animationTimer.start();
			//Exit program when main window is closed
			primaryStage.setOnCloseRequest(this::exitProgram);
Jan Kožusznik's avatar
Jan Kožusznik committed
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Draws objects into the canvas. Put you code here. 
	 *
	 *@return      nothing
	 */
Jan Kožusznik's avatar
Jan Kožusznik committed
	private void drawScene(double deltaT) {
Jan Kožusznik's avatar
Jan Kožusznik committed
		//graphic context is used for a painting
		GraphicsContext gc = canvas.getGraphicsContext2D();
Jan Kožusznik's avatar
Jan Kožusznik committed
		
Jan Kožusznik's avatar
Jan Kožusznik committed
	}
	
	private void exitProgram(WindowEvent evt) {
Jan Kožusznik's avatar
Jan Kožusznik committed
		animationTimer.stop();
Jan Kožusznik's avatar
Jan Kožusznik committed
		System.exit(0);
	}
}