diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 210b19fff7db7570d4cba390ad44ff328fde7445..d1aaf7ced10f38fc6d2c6d35516dc8fa2f129568 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -50,12 +50,15 @@ public class App extends Application { *@return nothing */ private void drawScene() { + World world = new World(canvas.getWidth(), canvas.getHeight()); //graphic context is used for a painting GraphicsContext gc = canvas.getGraphicsContext2D(); int timeout = 10; while (!Routines.isEndOfThreadRequestedByJavaVM()) { + world.draw(gc); Routines.sleep(timeout); double deltaT = timeout / 1000.; + world.simulate(deltaT); } } diff --git a/src/main/java/lab/Bullet.java b/src/main/java/lab/Bullet.java new file mode 100644 index 0000000000000000000000000000000000000000..3dc4c1b9b23d5e15825d9053762b847dc0302e58 --- /dev/null +++ b/src/main/java/lab/Bullet.java @@ -0,0 +1,45 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class Bullet { + + private Point2D start; + + private Point2D position; + + private Point2D speed; + + private double size; + + private World world; + + private Point2D acc; + + public Bullet(World world, Point2D start, Point2D speed, double size) { + this.world = world; + this.start = start; + this.speed = speed; + this.size = size; + this.position = start; + this.acc = new Point2D(0, - World.GRAVITATION_ACCELERATION); + } + + + public void draw (GraphicsContext gc) { + gc.save(); + gc.setFill(Color.STEELBLUE); + Point2D p = world.transform(position); + gc.fillOval(p.getX() - size / 2, p.getY() - size / 2, size, size); + gc.restore(); + } + + public void simulate(double deltaT) { + + position = position.add(speed.multiply(deltaT)); + speed = speed.add(acc.multiply(deltaT)); + } + +} diff --git a/src/main/java/lab/Canon.java b/src/main/java/lab/Canon.java new file mode 100644 index 0000000000000000000000000000000000000000..a7ebe5cc80575b5ead358a44fe5916f27a461630 --- /dev/null +++ b/src/main/java/lab/Canon.java @@ -0,0 +1,21 @@ +package lab; + + + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.transform.Affine; +import javafx.scene.transform.Transform; + +public class Canon { + + private int angle; + private Point2D position; + + public void draw(GraphicsContext gc) { + gc.save(); + gc.transform(new Affine(Transform.rotate(angle, position.getX(), position.getY()))); + gc.fillRect(position.getX(), position.getY(), 40, 20); + gc.restore(); + } +} diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java new file mode 100644 index 0000000000000000000000000000000000000000..6b3c6913bf12949c2e8da5c261645a8ced427724 --- /dev/null +++ b/src/main/java/lab/World.java @@ -0,0 +1,42 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class World { + + public static double GRAVITATION_ACCELERATION = 9.81; + + public double width; + + public double height; + + private Bullet bullet; + + private Canon canon; + + public World(double width, double height) { + this.width = width; + this.height = height; + this.bullet = new Bullet(this, new Point2D(10, 11), new Point2D(50,30), 10); + this.canon = new Canon(); + } + + public void draw(GraphicsContext gc) { + gc.save(); + gc.setFill(Color.LIGHTYELLOW); + gc.fillRect(0, 0, width, height); + gc.restore(); + this.bullet.draw(gc); + this.canon.draw(gc); + } + + public void simulate(double deltaT) { + bullet.simulate(deltaT); + } + + public Point2D transform(Point2D position) { + return new Point2D(position.getX(), height - position.getY()); + } +} \ No newline at end of file