From d77b491bf8f1b807e9ca26ce9b504cd1e94ce71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Wed, 22 Sep 2021 12:21:27 +0200 Subject: [PATCH] solution --- src/main/java/lab/App.java | 3 +++ src/main/java/lab/Bullet.java | 41 ++++++++++++++++++++++++++++++++++ src/main/java/lab/Canon.java | 27 ++++++++++++++++++++++ src/main/java/lab/World.java | 42 +++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 src/main/java/lab/Bullet.java create mode 100644 src/main/java/lab/Canon.java create mode 100644 src/main/java/lab/World.java diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 210b19f..3bef520 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -52,10 +52,13 @@ public class App extends Application { private void drawScene() { //graphic context is used for a painting GraphicsContext gc = canvas.getGraphicsContext2D(); + World world = new World(canvas.getWidth(), canvas.getHeight()); 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 0000000..f41921c --- /dev/null +++ b/src/main/java/lab/Bullet.java @@ -0,0 +1,41 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.Image; +import javafx.scene.paint.Color; + +public class Bullet { + private World world; + private Point2D startPosition; + private Point2D velocity; + private double size; + private Point2D position; + private Image image; + public Bullet(World world, Point2D startPosition, Point2D velocity, double size) { + super(); + this.world = world; + this.startPosition = startPosition; + this.velocity = velocity; + this.size = size; + this.position = startPosition; + image = new Image(Bullet.class.getResourceAsStream("fireball-transparent.gif")); + + } + + 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.drawImage(image, 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/Canon.java b/src/main/java/lab/Canon.java new file mode 100644 index 0000000..36646c7 --- /dev/null +++ b/src/main/java/lab/Canon.java @@ -0,0 +1,27 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; +import javafx.scene.transform.Affine; +import javafx.scene.transform.Transform; + +public class Canon { + + private Point2D position = new Point2D(10, 30); + + private int angle = 20; + + public void draw(GraphicsContext gc) { + gc.save(); + gc.setFill(Color.STEELBLUE); + gc.setTransform(new Affine(Transform.rotate(angle, position.getX(), position.getY() + 10))); + gc.fillRect(position.getX(), position.getY() + 6, 40, 12); + gc.restore(); + } + + + public void simulate(double deltaT) { + angle+= 5; + } +} diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java new file mode 100644 index 0000000..3d80190 --- /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 { + + private double width; + + private 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, 10), new Point2D(10, 30), 40); + this.canon = new Canon(); + } + + 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); + canon.draw(gc); + } + + public void simulate(double deltaT) { + this.bullet.simulate(deltaT); + this.canon.simulate(deltaT); + } +} -- GitLab