diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 3a9e838f99d4c9e57926a33c1d4ffe23ef873f42..58fd3f37f039d8171036be098a809c2ee811ccd6 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -9,22 +9,24 @@ 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 + * 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; private AnimationTimer timer; - + @Override public void start(Stage primaryStage) { try { - //Construct a main window with a canvas. + // Construct a main window with a canvas. Group root = new Group(); canvas = new Canvas(800, 400); root.getChildren().add(canvas); @@ -34,8 +36,8 @@ public class App extends Application { primaryStage.resizableProperty().set(false); primaryStage.setTitle("Java 1 - 1th laboratory"); primaryStage.show(); - - //Exit program when main window is closed + + // Exit program when main window is closed primaryStage.setOnCloseRequest(this::exitProgram); timer = new DrawingThread(canvas); timer.start(); @@ -43,12 +45,13 @@ public class App extends Application { e.printStackTrace(); } } + @Override public void stop() throws Exception { timer.stop(); super.stop(); } - + private void exitProgram(WindowEvent evt) { System.exit(0); } diff --git a/src/main/java/lab/Bullet.java b/src/main/java/lab/Bullet.java new file mode 100644 index 0000000000000000000000000000000000000000..30f7cdbaafcc901c4f8e2f6421dc8b838329014a --- /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 static final double SIZE = 20; + + private final Point2D acceleration; + + private Point2D position; + private Point2D velocity; + + public Bullet(Point2D position, Point2D velocity, Point2D acceleration) { + this.position = position; + this.velocity = velocity; + this.acceleration = acceleration; + } + + public void draw(GraphicsContext gc) { + gc.setFill(Color.SILVER); + gc.fillOval(position.getX(), position.getY(), SIZE, SIZE); + } + + public void simulate(double deltaT) { + position = position.add(velocity.multiply(deltaT)); + velocity = velocity.add(acceleration.multiply(deltaT)); + } + + protected Point2D getPosition() { + return position; + } +} diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java new file mode 100644 index 0000000000000000000000000000000000000000..e3aef872f6bf27650890daadaa0618d580cde57f --- /dev/null +++ b/src/main/java/lab/BulletAnimated.java @@ -0,0 +1,36 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.Image; + +public class BulletAnimated { + + private static final double SIZE = 40; + private Image image = new Image(this.getClass().getResourceAsStream("fireball-transparent.gif")); + + public BulletAnimated(Point2D position, Point2D velocity, Point2D acceleration) { + this.position = position; + this.velocity = velocity; + this.acceleration = acceleration; + } + + public void draw(GraphicsContext gc) { + gc.drawImage(image, getPosition().getX(), getPosition().getY(), SIZE, SIZE); + } + + private final Point2D acceleration; + + private Point2D position; + private Point2D velocity; + + public void simulate(double deltaT) { + position = position.add(velocity.multiply(deltaT)); + velocity = velocity.add(acceleration.multiply(deltaT)); + } + + protected Point2D getPosition() { + return position; + } + +} diff --git a/src/main/java/lab/Cannon.java b/src/main/java/lab/Cannon.java new file mode 100644 index 0000000000000000000000000000000000000000..70c5a995165f4f220acae8443f0fb104ded7860e --- /dev/null +++ b/src/main/java/lab/Cannon.java @@ -0,0 +1,33 @@ +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 Cannon { + + private static final double LENGTH = 60; + private static final double WIDTH = 15; + private Point2D position; + private double angle; + + public Cannon(Point2D position, double angle) { + this.position = position; + this.angle = angle; + } + + public void draw(GraphicsContext gc) { + gc.save(); + gc.transform(new Affine(Transform.rotate(angle, position.getX(), position.getY()))); + gc.setFill(Color.BROWN); + gc.fillRect(position.getX(), position.getY(), LENGTH, WIDTH); + gc.restore(); + } + + public void simulate(double deltaT) { + // do nothing yet + angle += 25 * deltaT; + } +} diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java index 1243331c3822cf37a2932e9e8eeb8f336e3de58d..50574b1f954dbf67486a4cc0d385b46420cdd128 100644 --- a/src/main/java/lab/DrawingThread.java +++ b/src/main/java/lab/DrawingThread.java @@ -8,23 +8,27 @@ public class DrawingThread extends AnimationTimer { private final Canvas canvas; private final GraphicsContext gc; + private final World world; + private long lastTime; public DrawingThread(Canvas canvas) { this.canvas = canvas; this.gc = canvas.getGraphicsContext2D(); - + world = new World(canvas.getWidth(), canvas.getHeight()); + lastTime = System.nanoTime(); } /** - * Draws objects into the canvas. Put you code here. + * Draws objects into the canvas. Put you code here. */ @Override public void handle(long now) { - // put your code here -// gc.setFill(Color.AQUA); -// gc.setStroke(Color.BLACK); -// gc.fillOval(10, 10, 20, 20); - + double deltaT = (now - lastTime) / 1e9; + // call draw on world + this.world.draw(gc); + // call simulate on world + this.world.simulate(deltaT); + lastTime = now; } } diff --git a/src/main/java/lab/World.java b/src/main/java/lab/World.java new file mode 100644 index 0000000000000000000000000000000000000000..d55ef2415049987e14fca2179f336cc8cbcd5e97 --- /dev/null +++ b/src/main/java/lab/World.java @@ -0,0 +1,42 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; + +public class World { + + private final double width; + + private final double hight; + + private final Bullet bullet; + private final BulletAnimated bullet2; + private final Cannon cannon; + + public World(double width, double hight) { + this.width = width; + this.hight = hight; + this.bullet = new Bullet(new Point2D(0, 0), new Point2D(30, 30), new Point2D(0, -9.81)); + this.bullet2 = new BulletAnimated(new Point2D(0, 0), new Point2D(30, 70), new Point2D(0, -9.81)); + this.cannon = new Cannon(new Point2D(0, 0), 45); + } + + public void draw(GraphicsContext gc) { + gc.clearRect(0, 0, width, hight); + + gc.save(); + // Change coordinate system to human like + gc.scale(1, -1); + gc.translate(0, -hight); + this.bullet.draw(gc); + this.bullet2.draw(gc); + this.cannon.draw(gc); + gc.restore(); + } + + public void simulate(double deltaT) { + this.bullet.simulate(deltaT); + this.bullet2.simulate(deltaT); + this.cannon.simulate(deltaT); + } +} \ No newline at end of file diff --git a/src/main/resources/lab/fireball-transparent.gif b/src/main/resources/lab/fireball-transparent.gif new file mode 100644 index 0000000000000000000000000000000000000000..ed734a665d6208de17057a378b4d4520126301b5 Binary files /dev/null and b/src/main/resources/lab/fireball-transparent.gif differ diff --git a/src/main/resources/lab/scenery.gif b/src/main/resources/lab/scenery.gif new file mode 100644 index 0000000000000000000000000000000000000000..d65c8d3d921f20d9b41709b90e4c79e3a6bd6232 Binary files /dev/null and b/src/main/resources/lab/scenery.gif differ