diff --git a/src/main/java/lab/App.java b/src/main/java/lab/App.java index 60a62315354e511d7e10174962b05037fbb01fae..6104810e295929b766aa4c654079bb2c5ff0df52 100644 --- a/src/main/java/lab/App.java +++ b/src/main/java/lab/App.java @@ -5,6 +5,7 @@ import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; +import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.stage.WindowEvent; @@ -18,7 +19,6 @@ public class App extends Application { launch(args); } - private Canvas canvas; private AnimationTimer timer; @Override @@ -26,9 +26,10 @@ public class App extends Application { try { //Construct a main window with a canvas. Group root = new Group(); - canvas = new Canvas(800, 400); + Canvas canvas = new Canvas(800, 400); root.getChildren().add(canvas); Scene scene = new Scene(root, 800, 400); + scene.setFill(Color.BLANCHEDALMOND); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.resizableProperty().set(false); diff --git a/src/main/java/lab/Bullet.java b/src/main/java/lab/Bullet.java new file mode 100644 index 0000000000000000000000000000000000000000..85d5ce74746fa5a2dc76bac9d56b2ab778e9e1ee --- /dev/null +++ b/src/main/java/lab/Bullet.java @@ -0,0 +1,50 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; + +public class Bullet { + + private final World world; + private Point2D position; + private Point2D velocity; + private final double size; + + public Bullet(World world, Point2D position, Point2D velocity, double size) { + this.world = world; + this.position = position; + this.velocity = velocity; + this.size = size; + } + + public void simulate(double deltaT) { + position = position.add(velocity.multiply(deltaT)); + position = new Point2D((position.getX() + world.getWidth()) % world.getWidth(), (position.getY() + world.getHeight()) % world.getHeight()); + } + + public void draw(GraphicsContext gc) { + gc.setFill(Color.RED); + Point2D p = world.transform2Canvas(position); + gc.fillOval(p.getX(), p.getY(), size, size); + } + + protected World getWorld() { + return world; + } + + protected Point2D getPosition() { + return position; + } + + protected double getSize() { + return size; + } + + + + //simulate + + //draw + +} diff --git a/src/main/java/lab/BulletAnimated.java b/src/main/java/lab/BulletAnimated.java new file mode 100644 index 0000000000000000000000000000000000000000..dedbbdcfc13e8f65d2e105087caf28041ef17f34 --- /dev/null +++ b/src/main/java/lab/BulletAnimated.java @@ -0,0 +1,23 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.Image; + +public class BulletAnimated extends Bullet{ + + + public BulletAnimated(World world, Point2D position, Point2D velocity, double size) { + super(world, position, velocity, size); + } + + private Image image = new Image(getClass().getResourceAsStream("fireball-transparent.gif")); + //gc.drawImage(image, width, height) + + @Override + public void draw(GraphicsContext gc) { + Point2D p = getWorld().transform2Canvas(getPosition()); + gc.drawImage(image,p.getX(), p.getY(), getSize(), getSize()); + } + +} diff --git a/src/main/java/lab/Cannon.java b/src/main/java/lab/Cannon.java new file mode 100644 index 0000000000000000000000000000000000000000..7926c79eaa4aa05be9f137fb1d16defb06766098 --- /dev/null +++ b/src/main/java/lab/Cannon.java @@ -0,0 +1,44 @@ +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 World world; + private final Point2D position; + private double angle; + private int speed = 10; + + public Cannon(World world, Point2D position, double angle) { + this.world = world; + this.position = position; + this.angle = angle; + } + + public void draw(GraphicsContext gc) { + gc.save(); + gc.setFill(Color.LIGHTSTEELBLUE); + Point2D p = world.transform2Canvas(position); + gc.setTransform(new Affine(Transform.rotate(-angle, p.getX(), p.getY()))); + gc.fillRect(p.getX(), p.getY(), 100, 20); + gc.restore(); + } + + public void simulate(double deltaT) { + angle = angle + speed*deltaT; + if (angle > 90) { + angle = 180 - angle; + speed *= -1; + } else if (angle < 0){ + angle = -angle; + speed *= -1; + } + } + + + +} diff --git a/src/main/java/lab/DrawingThread.java b/src/main/java/lab/DrawingThread.java index f2436cda938c0cee27acd4e925cd42dedd975197..bbf8f05b129b4e9c47544e12693370dfe9675477 100644 --- a/src/main/java/lab/DrawingThread.java +++ b/src/main/java/lab/DrawingThread.java @@ -8,9 +8,11 @@ public class DrawingThread extends AnimationTimer { private final GraphicsContext gc; private long lastTime; - + private final World world; + public DrawingThread(Canvas canvas) { this.gc = canvas.getGraphicsContext2D(); + this.world = new World(canvas.getWidth(), canvas.getHeight()); } /** @@ -21,8 +23,10 @@ public class DrawingThread extends AnimationTimer { if (lastTime > 0) { double deltaT = (now - lastTime) / 1e9; // call simulate on world + world.simulate(deltaT); } //call draw on world + 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..a35baa9662449a561776373dc05f41c245dd142f --- /dev/null +++ b/src/main/java/lab/World.java @@ -0,0 +1,46 @@ +package lab; + +import javafx.geometry.Point2D; +import javafx.scene.canvas.GraphicsContext; + +public class World { + private final double width; + + private final double height; + + private final Bullet bullet; + + private final Cannon cannon; + + public World(double width, double height) { + this.width = width; + this.height = height; + this.bullet = new BulletAnimated(this, new Point2D(50, 50), new Point2D(100, 100), 30); + this.cannon = new Cannon(this, new Point2D(50, 50), 45); + } + + public void simulate(double deltaT) { + this.bullet.simulate(deltaT); + this.cannon.simulate(deltaT); + } + + public void draw(GraphicsContext gc) { + gc.clearRect(0, 0, width, height); + this.bullet.draw(gc); + this.cannon.draw(gc); + } + + public Point2D transform2Canvas(Point2D p) { + return new Point2D(p.getX(), height - p.getY()); + } + + public double getWidth() { + return width; + } + + public double getHeight() { + return height; + } + + +}