Skip to content
Snippets Groups Projects
Commit 4d4c762b authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

lab03

parent e002db0c
Branches
No related merge requests found
......@@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vsb-cs-java1</groupId>
<artifactId>lab02</artifactId>
<artifactId>lab03</artifactId>
<version>0.0.1-SNAPHOST</version>
<packaging>jar</packaging>
<properties>
......
......@@ -4,7 +4,6 @@ import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
......@@ -31,7 +30,7 @@ public class App extends Application {
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.resizableProperty().set(false);
primaryStage.setTitle("Java 1 - 2nd laboratory");
primaryStage.setTitle("Java 1 - 3rd laboratory");
primaryStage.show();
//Exit program when main window is closed
......@@ -50,13 +49,7 @@ public class App extends Application {
*@return nothing
*/
private void drawScene() {
//graphic context is used for a painting
GraphicsContext gc = canvas.getGraphicsContext2D();
int timeout = 200;
while (Routines.isEndOfThreadRequestedByJavaVM()) {
Routines.sleep(timeout);
double deltaT = timeout / 1000.;
}
new Laboratory().draw(canvas);
}
private void exitProgram(WindowEvent evt) {
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated {
private Point2D position;
private Point2D start;
private Point2D speed;
private Point2D initialSpeed;
private double size;
private double mass = 2;
private double strenghtOfCannon = 2;
private double cannonLength = 100;
private boolean accelerate = true;
private boolean hitToGround = false;
private double crossSectionalArea;
private double dragCoefficient = 0.47;
private Image image;
private World world;
private Cannon cannon;
public BulletAnimated(World world, Cannon cannon) {
this(world, cannon, new Point2D(0, 0), new Point2D(0, 0), 10);
}
public BulletAnimated(World world, Cannon cannon, Point2D start, Point2D speed, double size) {
this.start = start;
this.position = this.start;
this.initialSpeed = speed;
this.speed = speed;
this.size = size;
this.world = world;
this.cannon = cannon;
image = new Image(getClass().getResourceAsStream("fireball-transparent.gif"), size, size,
true, true);
}
public void draw(GraphicsContext gc) {
gc.save();
Point2D canvasPosition = world.getCanvasPoint(position);
gc.drawImage(image, canvasPosition.getX(), canvasPosition.getY());
gc.restore();
}
public void simulate(double timeStep) {
if (accelerate && start.distance(position) < cannonLength) {
double cannonAngle = cannon.getAngle();
speed = speed
.add(new Point2D(Math.cos(cannonAngle) * strenghtOfCannon, Math.sin(cannonAngle) * strenghtOfCannon)
.multiply(1 / mass));
} else if (!hitToGround) {
accelerate = false;
Point2D airResistanceforce = new Point2D(
-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * dragCoefficient * Math.pow(speed.getX(), 2),
-1. / 2 * crossSectionalArea * Constants.AIR_DENSITY * 0.47 * Math.pow(speed.getY(), 2));
Point2D acceleration = new Point2D(-airResistanceforce.getX() * mass,
(-Constants.GRAVITATIONAL_ACCELERATION + airResistanceforce.getY()) * mass);
speed = speed.add(acceleration.multiply(timeStep / 1000));
}
if (!hitToGround) {
position = position.add(speed);
if (!accelerate && position.getY() <= size / 2) {
hitToGround = true;
position = new Point2D(position.getX(), size / 2);
}
} else {
reload();
}
}
public void reload() {
position = start;
speed = initialSpeed;
hitToGround = false;
accelerate = true;
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.transform.Affine;
public class Cannon {
private int direction=-1;
private double angle = 0;
private Point2D position;
private Point2D size;
private World world;
public Cannon(World world, Point2D position, Point2D size) {
super();
this.world = world;
this.position = position;
this.size = size;
}
public void simulate(double timeStep) {
angle = angle + direction*0.8;
if(angle <=-90 || angle >= 0) {
direction*=-1;
}
}
public void draw(GraphicsContext gc) {
gc.save();
Point2D worldPosition = world.getCanvasPoint(position);
gc.setFill(Color.BROWN);
gc.fillRect(worldPosition.getX()-10, worldPosition.getY()+size.getY(), size.getX()+20, size.getY()/2);
gc.fillOval(worldPosition.getX()-20, worldPosition.getY()+size.getY()/2, 40, 40);
gc.fillOval(worldPosition.getX()+size.getX(), worldPosition.getY()+size.getY()/2, 40, 40);
gc.setTransform(new Affine(Affine.rotate(angle, worldPosition.getX(), worldPosition.getY()+size.getY()/2)));
gc.setFill(Color.BLACK);
gc.fillRect(worldPosition.getX(), worldPosition.getY(), size.getX(), size.getY());
gc.restore();
}
public double getAngle() {
return (angle * -1) / 180 * Math.PI;
}
}
package lab;
import javafx.scene.image.Image;
public final class Constants {
private Constants() {}
public static final double GRAVITATIONAL_ACCELERATION = 9.81;
public static final double AIR_DENSITY = 1.2;
static final int REFRESH_TIMEOUT = 25;
public static final Image DRAGON_IMAGE;
static{
DRAGON_IMAGE = new Image(Constants.class.getResourceAsStream("dragon.gif"));
}
}
package lab;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class Laboratory {
public void draw(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D();
World world = new World(canvas.getWidth(), canvas.getHeight());
while (!Routines.isEndOfThreadRequestedByJavaVM()) {
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
world.draw(canvas);
Routines.sleep(25);
world.simulate(25);
}
}
}
package lab;
public class Routines {
public final class Routines {
private Routines() { }
public static void sleep(int timeInMilisenonds) {
try {
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
public class World {
private double width;
private double height;
private BulletAnimated bulletAnimatted;
private Cannon cannon;
public World(double width, double height) {
super();
this.width = width;
this.height = height;
cannon = new Cannon(this, new Point2D(50, 50), new Point2D(100, 20));
bulletAnimatted = new BulletAnimated(this, cannon, new Point2D(30, 60), new Point2D(0, 0), 40);
}
public Point2D getCanvasPoint(Point2D worldPoint) {
return new Point2D(worldPoint.getX(), height - worldPoint.getY());
}
public void draw(Canvas canvas) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
cannon.draw(gc);
bulletAnimatted.draw(gc);
}
public void simulate(int timeDelta) {
bulletAnimatted.simulate(timeDelta);
cannon.simulate(timeDelta);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
src/main/resources/lab/dragon.gif

86.9 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