diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 210b19fff7db7570d4cba390ad44ff328fde7445..60a62315354e511d7e10174962b05037fbb01fae 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -1,10 +1,10 @@ package lab; +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.stage.Stage; import javafx.stage.WindowEvent; @@ -19,6 +19,7 @@ public class App extends Application { } private Canvas canvas; + private AnimationTimer timer; @Override public void start(Stage primaryStage) { @@ -36,27 +37,16 @@ public class App extends Application { //Exit program when main window is closed primaryStage.setOnCloseRequest(this::exitProgram); - - //Draw scene on a separate thread to avoid blocking UI. - new Thread(this::drawScene).start(); + timer = new DrawingThread(canvas); + timer.start(); } catch (Exception e) { e.printStackTrace(); } } - - /** - * Draws objects into the canvas. Put you code here. - * - *@return nothing - */ - private void drawScene() { - //graphic context is used for a painting - GraphicsContext gc = canvas.getGraphicsContext2D(); - int timeout = 10; - while (!Routines.isEndOfThreadRequestedByJavaVM()) { - Routines.sleep(timeout); - double deltaT = timeout / 1000.; - } + @Override + public void stop() throws Exception { + timer.stop(); + super.stop(); } private void exitProgram(WindowEvent evt) { diff --git a/src/main/java/lab/Bullet.java b/src/main/java/lab/Bullet.java new file mode 100644 index 0000000000000000000000000000000000000000..67103902ab17355a24315a493e1abbc5f24b5b31 --- /dev/null +++ b/src/main/java/lab/Bullet.java @@ -0,0 +1,34 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class Bullet { + private World world; + private Point2D velocity; + private double size; + private Point2D position; + public Bullet(World world, Point2D startPosition, Point2D velocity, double size) { + super(); + this.world = world; + this.velocity = velocity; + this.size = size; + this.position = startPosition; + } + + public void draw(GraphicsContext gc) { + gc.save(); + gc.setFill(Color.BROWN); + Point2D p = world.getCanvasPoint(position); + gc.fillOval(p.getX() - size /2, p.getY() - size/2, size, size); + gc.restore(); + } + + public void simulate(double deltaT) { + // TODO Auto-generated method stub + position = position.add(velocity.multiply(deltaT)); + } + + +} diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java new file mode 100644 index 0000000000000000000000000000000000000000..4a7519fcf871d1a043c2d56cf17bf0bf6dd556b5 --- /dev/null +++ b/src/main/java/lab/DrawingThread.java @@ -0,0 +1,32 @@ +package lab; + +import javafx.animation.AnimationTimer; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; + +public class DrawingThread extends AnimationTimer { + + private final GraphicsContext gc; + private final World world; + private long lastTime; + + public DrawingThread(Canvas canvas) { + this.gc = canvas.getGraphicsContext2D(); + this.world = new World(canvas.getWidth(), canvas.getHeight()); + } + + /** + * Draws objects into the canvas. Put you code here. + */ + @Override + public void handle(long now) { + if (lastTime > 0) { + double deltaT = (now - lastTime) / 1e9; + world.simulate(deltaT); + } + world.draw(gc); + lastTime= now; + + } + +} diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java new file mode 100644 index 0000000000000000000000000000000000000000..8cef4e9f53881a03fbbd71069463f9cdb7e7c158 --- /dev/null +++ b/src/main/java/lab/World.java @@ -0,0 +1,38 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class World { + + private double width; + + private double height; + + private Bullet bullet; + + + public World(double width, double height) { + this.width = width; + this.height = height; + this.bullet = new Bullet(this, new Point2D(10, 10), new Point2D(10, 30), 15); + } + + public Point2D getCanvasPoint(Point2D in) { + Point2D result = new Point2D(in.getX(), height - in.getY()); + return result; + } + + public void draw(GraphicsContext gc) { + gc.save(); + gc.setFill(Color.LIGHTGREY); + gc.fillRect(0, 0, width, height); + gc.restore(); + bullet.draw(gc); + } + + public void simulate(double deltaT) { + this.bullet.simulate(deltaT); + } +}