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

feat: solution

parent a43deb92
No related merge requests found
......@@ -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.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 {
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) {
this.position = position;
this.velocity = velocity;
this.acceleration = acceleration;
}
public void draw(GraphicsContext gc) {
gc.drawImage(image, getPosition().getX(), getPosition().getY(), SIZE, SIZE);
}
private final Point2D acceleration;
private Point2D position;
private Point2D velocity;
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.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.BROWN);
gc.fillRect(position.getX(), position.getY(), LENGTH, WIDTH);
gc.restore();
}
public void simulate(double deltaT) {
// do nothing yet
angle += 25 * deltaT;
}
}
......@@ -8,23 +8,27 @@ public class DrawingThread extends AnimationTimer {
private final Canvas canvas;
private final GraphicsContext gc;
private final World world;
private long lastTime;
public DrawingThread(Canvas canvas) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
world = new World(canvas.getWidth(), canvas.getHeight());
lastTime = System.nanoTime();
}
/**
* Draws objects into the canvas. Put you code here.
* Draws objects into the canvas. Put you code here.
*/
@Override
public void handle(long now) {
// put your code here
// gc.setFill(Color.AQUA);
// gc.setStroke(Color.BLACK);
// gc.fillOval(10, 10, 20, 20);
double deltaT = (now - lastTime) / 1e9;
// call draw on world
this.world.draw(gc);
// call simulate on world
this.world.simulate(deltaT);
lastTime = now;
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public class World {
private final double width;
private final double hight;
private final Bullet bullet;
private final BulletAnimated 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.clearRect(0, 0, width, hight);
gc.save();
// Change coordinate system to human like
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/fireball-transparent.gif

412 KiB

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