Skip to content
Snippets Groups Projects
Commit 15b6fca4 authored by koz01's avatar koz01
Browse files

Solution

parent eabbd34d
No related merge requests found
Pipeline #66 failed with stages
in 0 seconds
......@@ -2,3 +2,4 @@
.settings/
.project
.classpath
/bin/
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Bullet {
private final World world;
private final Point2D startPosition;
private Point2D position;
private Point2D velocity;
private final double size;
public Bullet(World world, Point2D startPosition, Point2D velocity, double size) {
this.world = world;
this.startPosition = startPosition;
this.position = startPosition;
this.velocity = velocity;
this.size = size;
}
public void draw(GraphicsContext gc) {
gc.setFill(Color.RED);
Point2D p = world.getPositionInCanvas(position);
gc.fillOval(p.getX(), p.getY(), size, size);
}
public void simulate(double deltaT) {
position = position.add(velocity.multiply(deltaT));
position = new Point2D((position.getX() + world.getWidht()) % world.getWidht(),
(position.getY() + world.getHeight()) % world.getHeight());
}
}
package lab;
import javafx.scene.image.Image;
public class BulletAnimated {
private final Image image = new Image(this.getClass().getResourceAsStream("fireball-transparent.gif"));
}
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 double angle;
private final Point2D position;
private final World world;
public Cannon(World world,int angle, Point2D position) {
this.world = world;
this.angle = angle;
this.position = position;
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.STEELBLUE);
Point2D p = world.getPositionInCanvas(position);
gc.transform(new Affine(Transform.rotate(angle - 90, p.getX(), p.getY())));
gc.fillRect(p.getX(), p.getY(), 100, 10);
gc.restore();
}
public void simulate(double deltaT) {
angle = angle + 10*deltaT;
}
}
......@@ -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());
}
/**
......@@ -20,11 +22,11 @@ public class DrawingThread extends AnimationTimer {
public void handle(long now) {
if (lastTime > 0) {
double deltaT = (now - lastTime) / 1e9;
// call simulate on world
world.simulate(deltaT);
}
//call draw on world
world.draw(gc);
lastTime= now;
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public class World {
private double width;
private 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 Bullet(this, new Point2D(25, 150), new Point2D(100, 100), 30);
this.cannon = new Cannon(this, 45, new Point2D(20, 20));
}
public void simulate(double deltaT) {
bullet.simulate(deltaT);
cannon.simulate(deltaT);
}
public void draw(GraphicsContext gc) {
gc.clearRect(0, 0, width, height);
bullet.draw(gc);
cannon.draw(gc);
}
public Point2D getPositionInCanvas(Point2D point) {
return new Point2D(point.getX(), height - point.getY());
}
public double getWidht() {
return width;
}
public double getHeight() {
return height;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment