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

solution

parent eabbd34d
No related merge requests found
Pipeline #65 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 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.transform2Canvas(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.getWidth()) % world.getWidth(), (position.getY() + world.getHeight()) % world.getHeight());
}
}
package lab;
import javafx.scene.image.Image;
public class BulletAnimated {
private final Image image = new Image(getClass().getResourceAsStream("fireball-transparent.gif"));
//gc.drawImage(image, width, height) ...
}
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 double w = 10;
private final Point2D position;
private final World world;
public Cannon(World world,double 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.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 + w * deltaT;
}
}
......@@ -7,10 +7,14 @@ import javafx.scene.canvas.GraphicsContext;
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 +25,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;
}
......
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 Bullet(this, new Point2D(100, 50), new Point2D(100, -100), 20);
this.cannon = new Cannon(this, 45, new Point2D(50, 50));
}
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 point) {
return new Point2D(point.getX(), height - point.getY());
}
public double getWidth() {
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