Skip to content
Snippets Groups Projects
Commit 9e34e065 authored by jez04's avatar jez04
Browse files

feat: solution

parent 209b2283
Branches solution
No related merge requests found
package lab;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
......@@ -18,9 +19,14 @@ public class Boat {
public void draw(GraphicsContext gc) {
gc.drawImage(image, position.getX(), position.getY());
gc.strokeRect(position.getX(), position.getY(), image.getWidth(), image.getHeight());
}
public void simulate(double deltaTime) {
}
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), image.getWidth(), image.getHeight());
}
}
......@@ -10,16 +10,16 @@ public class DrawingThread extends AnimationTimer {
private final Canvas canvas;
private final GraphicsContext gc;
private Scene scene;
private long lastSecond = 0;;
private int frameCount = 0;
private int fps = 0;
private long lastTime;
private long lastSecond = 0;
public DrawingThread(Canvas canvas) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
scene = new Scene(canvas.getWidth(), canvas.getHeight());
lastSecond = System.nanoTime();
lastTime = System.nanoTime();
}
/**
......
package lab;
import java.util.Random;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class LochNess {
private static final Random RANDOM = new Random();
private Scene scene;
private Point2D position;
private Point2D speed;
private Image image;
public LochNess(Scene scene) {
this.scene = scene;
image = new Image(LochNess.class.getResourceAsStream("LochNess.gif"));
position = new Point2D(RANDOM.nextDouble(scene.getSize().getWidth() * 0.3, scene.getSize().getWidth()),
RANDOM.nextDouble(scene.getSize().getHeight() * 0.5, scene.getSize().getHeight() - image.getHeight()));
speed = new Point2D(-RANDOM.nextDouble(50, 150), 0);
}
public void draw(GraphicsContext gc) {
gc.drawImage(image, position.getX(), position.getY());
gc.strokeRect(position.getX(), position.getY(), image.getWidth(), image.getHeight());
}
public void simulate(double deltaTime) {
position = position.add(speed.multiply(deltaTime / 1_000_000_000));
if (position.getX() + image.getWidth() < 0) {
position = new Point2D(scene.getSize().getWidth(), position.getY());
}
if (position.getX() > scene.getSize().getWidth()+5) {
position = new Point2D(scene.getSize().getWidth(), position.getY());
speed = speed.multiply(-1);
}
}
public Rectangle2D getBoundingBox() {
return new Rectangle2D(position.getX(), position.getY(), image.getWidth(), image.getHeight());
}
public void changeDirection() {
speed = speed.multiply(-1);
}
}
......@@ -28,7 +28,7 @@ public class Rock {
gc.save();
gc.setFill(Color.GRAY);
gc.setStroke(Color.GREEN);
Point2D center = position.add(size.getWidth()/2, size.getHeight()/2);
Point2D center = position.add(size.getWidth() / 2, size.getHeight() / 2);
Rotate rotateMatrix = Affine.rotate(angle, center.getX(), center.getY());
gc.setTransform(new Affine(rotateMatrix));
gc.fillRect(position.getX(), position.getY(), size.getWidth(), size.getHeight());
......
......@@ -10,28 +10,42 @@ public class Scene {
private Background background;
private Rock rock;
private Boat boat;
private LochNess[] lochNesses;
public Scene(double width, double height) {
size = new Dimension2D(width, height);
background = new Background(this);
rock = new Rock(this, new Point2D(300, 300), new Dimension2D(30, 50));
boat = new Boat(this, new Point2D(20, 200));
lochNesses = new LochNess[5];
for (int i = 0; i < lochNesses.length; i++) {
lochNesses[i] = new LochNess(this);
}
}
public Dimension2D getSize() {
return size;
}
public void draw(GraphicsContext gc) {
background.draw(gc);
rock.draw(gc);
boat.draw(gc);
}
}
public void draw(GraphicsContext gc) {
background.draw(gc);
rock.draw(gc);
boat.draw(gc);
for (LochNess lochNess : lochNesses) {
lochNess.draw(gc);
}
}
public void simulate(double deltaTime) {
background.simulate(deltaTime);
rock.simulate(deltaTime);
boat.simulate(deltaTime);
for (LochNess lochNess : lochNesses) {
lochNess.simulate(deltaTime);
if (lochNess.getBoundingBox().intersects(boat.getBoundingBox())) {
lochNess.changeDirection();
}
}
}
}
src/main/resources/lab/LochNess.gif

2.25 KiB

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