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

solution

parent eabbd34d
No related merge requests found
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;
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated extends Bullet{
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) {
super(position, velocity, acceleration);
}
@Override
public void draw(GraphicsContext gc) {
gc.drawImage(image, getPosition().getX(), getPosition().getY(), SIZE, SIZE);
}
}
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.LIGHTSTEELBLUE);
gc.fillRect(position.getX(), position.getY(), LENGTH, WIDTH);
gc.restore();
}
public void simulate(double deltaT) {
//do nothing yet
angle += 25 * deltaT;
}
}
......@@ -7,10 +7,13 @@ import javafx.scene.canvas.GraphicsContext;
public class DrawingThread extends AnimationTimer {
private final GraphicsContext gc;
private final World world;
private long lastTime;
public DrawingThread(Canvas canvas) {
this.gc = canvas.getGraphicsContext2D();
this.world = new World(canvas.getWidth(), canvas.getHeight());
}
/**
......@@ -21,8 +24,10 @@ public class DrawingThread extends AnimationTimer {
if (lastTime > 0) {
double deltaT = (now - lastTime) / 1e9;
// call simulate on world
this.world.simulate(deltaT);
}
//call draw on world
this.world.draw(gc);
lastTime= now;
}
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class World {
private final double width;
private final double hight;
private final Image image = new Image(this.getClass().getResourceAsStream("scenery.gif"));
private final Bullet bullet;
private final Bullet 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.drawImage(image, 0, 0, width, hight);
gc.save();
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
src/main/resources/lab/scenery.gif

3.75 MiB

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