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

Solution

parent 4e818c66
No related merge requests found
......@@ -50,12 +50,15 @@ public class App extends Application {
*@return nothing
*/
private void drawScene() {
World world = new World(canvas.getWidth(), canvas.getHeight());
//graphic context is used for a painting
GraphicsContext gc = canvas.getGraphicsContext2D();
int timeout = 10;
while (!Routines.isEndOfThreadRequestedByJavaVM()) {
world.draw(gc);
Routines.sleep(timeout);
double deltaT = timeout / 1000.;
world.simulate(deltaT);
}
}
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Bullet {
private Point2D start;
private Point2D position;
private Point2D speed;
private double size;
private World world;
private Point2D acc;
public Bullet(World world, Point2D start, Point2D speed, double size) {
this.world = world;
this.start = start;
this.speed = speed;
this.size = size;
this.position = start;
this.acc = new Point2D(0, - World.GRAVITATION_ACCELERATION);
}
public void draw (GraphicsContext gc) {
gc.save();
gc.setFill(Color.STEELBLUE);
Point2D p = world.transform(position);
gc.fillOval(p.getX() - size / 2, p.getY() - size / 2, size, size);
gc.restore();
}
public void simulate(double deltaT) {
position = position.add(speed.multiply(deltaT));
speed = speed.add(acc.multiply(deltaT));
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Transform;
public class Canon {
private int angle;
private Point2D position;
public void draw(GraphicsContext gc) {
gc.save();
gc.transform(new Affine(Transform.rotate(angle, position.getX(), position.getY())));
gc.fillRect(position.getX(), position.getY(), 40, 20);
gc.restore();
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class World {
public static double GRAVITATION_ACCELERATION = 9.81;
public double width;
public double height;
private Bullet bullet;
private Canon canon;
public World(double width, double height) {
this.width = width;
this.height = height;
this.bullet = new Bullet(this, new Point2D(10, 11), new Point2D(50,30), 10);
this.canon = new Canon();
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.LIGHTYELLOW);
gc.fillRect(0, 0, width, height);
gc.restore();
this.bullet.draw(gc);
this.canon.draw(gc);
}
public void simulate(double deltaT) {
bullet.simulate(deltaT);
}
public Point2D transform(Point2D position) {
return new Point2D(position.getX(), height - position.getY());
}
}
\ No newline at end of file
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