Skip to content
Snippets Groups Projects
Commit 3ccabacb authored by jez04's avatar jez04
Browse files

feat: solution

parent a43deb92
Branches
No related merge requests found
Pipeline #2050 canceled with stages
......@@ -22,26 +22,36 @@
<artifactId>javafx-fxml</artifactId>
<version>23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<!--
https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<!--
https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<!--
https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
</dependencies>
</project>
......@@ -9,22 +9,24 @@ import javafx.stage.Stage;
import javafx.stage.WindowEvent;
/**
* Class <b>App</b> - extends class Application and it is an entry point of the program
* @author Java I
* Class <b>App</b> - extends class Application and it is an entry point of the
* program
*
* @author Java I
*/
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
private Canvas canvas;
private AnimationTimer timer;
@Override
public void start(Stage primaryStage) {
try {
//Construct a main window with a canvas.
// Construct a main window with a canvas.
Group root = new Group();
canvas = new Canvas(800, 400);
root.getChildren().add(canvas);
......@@ -34,8 +36,8 @@ public class App extends Application {
primaryStage.resizableProperty().set(false);
primaryStage.setTitle("Java 1 - 1th laboratory");
primaryStage.show();
//Exit program when main window is closed
// Exit program when main window is closed
primaryStage.setOnCloseRequest(this::exitProgram);
timer = new DrawingThread(canvas);
timer.start();
......@@ -43,12 +45,13 @@ public class App extends Application {
e.printStackTrace();
}
}
@Override
public void stop() throws Exception {
timer.stop();
super.stop();
}
private void exitProgram(WindowEvent evt) {
System.exit(0);
}
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
public class Background {
private Scene scene;
private Point2D[] imagePosition;
private Image[] image;
private Point2D speed;
private static final double[] speedMultiplier = new double[] { 0.6, 0.8, 1, 1 };
public Background(Scene scene) {
this.scene = scene;
image = new Image[4];
imagePosition = new Point2D[4];
image[0] = new Image(Background.class.getResourceAsStream("cityfar400.png"));
image[1] = new Image(Background.class.getResourceAsStream("citymid400.png"));
image[2] = new Image(Background.class.getResourceAsStream("cityclose400.png"));
image[3] = new Image(Background.class.getResourceAsStream("cityreflection400.png"));
imagePosition[0] = new Point2D(0, image[0].getHeight() - scene.getSize().getHeight());
imagePosition[1] = new Point2D(0, image[1].getHeight() - scene.getSize().getHeight());
imagePosition[2] = new Point2D(0, image[2].getHeight() - scene.getSize().getHeight());
imagePosition[3] = new Point2D(0, image[3].getHeight() - scene.getSize().getHeight());
speed = new Point2D(100, 0);
}
public void draw(GraphicsContext gc) {
gc.setFill(Color.DARKBLUE);
gc.fillRect(0, 0, scene.getSize().getWidth(), scene.getSize().getHeight());
for (int i = 0; i < image.length; i++) {
double w = scene.getSize().getWidth();
double x = imagePosition[i].getX();
double realWidth = x + w <= image[i].getWidth() ? w : image[i].getWidth() - x;
gc.drawImage(image[i], x, imagePosition[i].getY(), realWidth, scene.getSize().getHeight(), 0, 0, realWidth,
scene.getSize().getHeight());
if (x + w > image[i].getWidth()) {
gc.drawImage(image[i], 0, imagePosition[i].getY(), w - realWidth, scene.getSize().getHeight(),
image[i].getWidth() - imagePosition[i].getX(), 0, w - realWidth, scene.getSize().getHeight());
}
}
}
public void simulate(double deltaTime) {
Point2D baseSpeed = speed.multiply(deltaTime / 1_000_000_000);
for (int i = 0; i < image.length; i++) {
imagePosition[i] = imagePosition[i].add(baseSpeed.multiply(speedMultiplier[i]));
if (imagePosition[i].getX() > image[i].getWidth()) {
imagePosition[i] = new Point2D(0, imagePosition[i].getY());
}
}
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class Boat {
private Scene scene;
private Point2D position;
private Image image;
public Boat(Scene scene, Point2D position) {
this.scene = scene;
this.position = position;
image = new Image(Boat.class.getResourceAsStream("ship-boat.gif"));
}
public void draw(GraphicsContext gc) {
gc.drawImage(image, position.getX(), position.getY());
}
public void simulate(double deltaTime) {
}
}
......@@ -3,16 +3,23 @@ package lab;
import javafx.animation.AnimationTimer;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
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;
public DrawingThread(Canvas canvas) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
scene = new Scene(canvas.getWidth(), canvas.getHeight());
lastSecond = System.nanoTime();
}
/**
......@@ -20,11 +27,22 @@ public class DrawingThread extends AnimationTimer {
*/
@Override
public void handle(long now) {
// put your code here
// gc.setFill(Color.AQUA);
// gc.setStroke(Color.BLACK);
// gc.fillOval(10, 10, 20, 20);
long timeDelta = now - lastTime;
scene.draw(gc);
scene.simulate(timeDelta);
long currentSecond = now / 1_000_000_000;
if(lastSecond == currentSecond) {
frameCount ++;
} else {
lastSecond = currentSecond;
fps = frameCount;
frameCount = 0;
}
lastTime = now;
//print FPS
gc.setStroke(Color.RED);
gc.strokeText(Integer.toString(fps), 10, 30);
gc.strokeText(Double.toString(1_000_000_000d/timeDelta), 10, 50);
}
}
package lab;
import javafx.geometry.Dimension2D;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Rotate;
public class Rock {
private Scene scene;
private Point2D position;
private Dimension2D size;
private double angle = 0;
public Rock(Scene scene) {
this(scene, new Point2D(350, 200), new Dimension2D(60, 10));
}
public Rock(Scene scene, Point2D position, Dimension2D size) {
this.scene = scene;
this.position = position;
this.size = size;
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.GRAY);
gc.setStroke(Color.GREEN);
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());
gc.strokeRect(position.getX(), position.getY(), size.getWidth(), size.getHeight());
gc.restore();
}
public void simulate(double deltaTime) {
angle += 0.5;
}
}
package lab;
import javafx.geometry.Dimension2D;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public class Scene {
private Dimension2D size;
private Background background;
private Rock rock;
private Boat boat;
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));
}
public Dimension2D getSize() {
return size;
}
public void draw(GraphicsContext gc) {
background.draw(gc);
rock.draw(gc);
boat.draw(gc);
}
public void simulate(double deltaTime) {
background.simulate(deltaTime);
rock.simulate(deltaTime);
boat.simulate(deltaTime);
}
}
......@@ -2,6 +2,7 @@ module lab01 {
requires transitive javafx.controls;
requires javafx.fxml;
requires javafx.base;
requires org.reflections;
opens lab to javafx.fxml;
exports lab;
}
\ No newline at end of file
src/main/resources/lab/cityclose400.png

16.2 KiB

src/main/resources/lab/cityfar400.png

17.1 KiB

src/main/resources/lab/citymid400.png

15.9 KiB

src/main/resources/lab/cityreflection400.png

30.1 KiB

src/main/resources/lab/ship-boat.gif

12.8 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