Skip to content
Snippets Groups Projects
Commit 74a8d91c authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Lab05

Use GameController
parent 7b1c65c6
Branches master
No related merge requests found
README.md 100644 → 100755
File mode changed from 100644 to 100755
package lab; package lab;
import javafx.animation.AnimationTimer;
import javafx.application.Application; import javafx.application.Application;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.Scene; import javafx.scene.Scene;
...@@ -19,39 +18,25 @@ public class App extends Application { ...@@ -19,39 +18,25 @@ public class App extends Application {
} }
private Canvas canvas; private Canvas canvas;
private AnimationTimer animationTimer; private GameController controller;
private Laboratory lab;
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
try { try {
//Construct a main window with a canvas. //Construct a main window with a canvas.
Group root = new Group(); Group root = new Group();
canvas = new Canvas(800, 400); canvas = new Canvas(800, 600);
root.getChildren().add(canvas); root.getChildren().add(canvas);
Scene scene = new Scene(root, 800, 400); Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.resizableProperty().set(false); primaryStage.resizableProperty().set(false);
primaryStage.setTitle("Java 1 - 3rd laboratory"); primaryStage.setTitle("Java 1 - 6th laboratory");
primaryStage.show(); primaryStage.show();
controller = new GameController(canvas);
lab = new Laboratory(canvas); controller.startGame();
//Draw scene on a separate thread to avoid blocking UI.
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 //Exit program when main window is closed
primaryStage.setOnCloseRequest(this::exitProgram); primaryStage.setOnCloseRequest(this::exitProgram);
} catch (Exception e) { } catch (Exception e) {
...@@ -59,17 +44,10 @@ public class App extends Application { ...@@ -59,17 +44,10 @@ public class App extends Application {
} }
} }
/**
* Draws objects into the canvas. Put you code here.
*
*@return nothing
*/
private void drawScene(double deltaT) {
lab.draw(deltaT);
}
private void exitProgram(WindowEvent evt) { private void exitProgram(WindowEvent evt) {
animationTimer.stop(); controller.stopGame();
System.exit(0); System.exit(0);
} }
} }
\ No newline at end of file
package lab;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
public class GameController {
private World world;
private Canvas canvas;
private AnimationTimer animationTimer;
public GameController(Canvas canvas) {
this.canvas = canvas;
}
public void startGame() {
this.world = new World(canvas.getWidth(), canvas.getHeight());
//Draw scene on a separate thread to avoid blocking UI.
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();
}
public void stopGame() {
animationTimer.stop();
}
private void drawScene(double deltaT) {
world.draw(canvas);
world.simulate(deltaT);
}
}
package lab;
import javafx.scene.canvas.Canvas;
public class Laboratory {
private World world;
private Canvas canvas;
public Laboratory(Canvas canvas) {
this.canvas = canvas;
this.world = new World(canvas.getWidth(), canvas.getHeight());
}
public void draw(double deltaT) {
world.draw(canvas);
world.simulate(deltaT);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment