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

solution

parent 4e818c66
No related merge requests found
...@@ -52,10 +52,13 @@ public class App extends Application { ...@@ -52,10 +52,13 @@ public class App extends Application {
private void drawScene() { private void drawScene() {
//graphic context is used for a painting //graphic context is used for a painting
GraphicsContext gc = canvas.getGraphicsContext2D(); GraphicsContext gc = canvas.getGraphicsContext2D();
World world = new World(canvas.getWidth(), canvas.getHeight());
int timeout = 10; int timeout = 10;
while (!Routines.isEndOfThreadRequestedByJavaVM()) { while (!Routines.isEndOfThreadRequestedByJavaVM()) {
world.draw(gc);
Routines.sleep(timeout); Routines.sleep(timeout);
double deltaT = timeout / 1000.; double deltaT = timeout / 1000.;
world.simulate(deltaT);
} }
} }
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
public class Bullet {
private World world;
private Point2D startPosition;
private Point2D velocity;
private double size;
private Point2D position;
private Image image;
public Bullet(World world, Point2D startPosition, Point2D velocity, double size) {
super();
this.world = world;
this.startPosition = startPosition;
this.velocity = velocity;
this.size = size;
this.position = startPosition;
image = new Image(Bullet.class.getResourceAsStream("fireball-transparent.gif"));
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.BROWN);
Point2D p = world.getCanvasPoint(position);
//gc.fillOval(p.getX() - size /2, p.getY() - size/2, size, size);
gc.drawImage(image, p.getX() - size /2, p.getY() - size/2, size, size);
gc.restore();
}
public void simulate(double deltaT) {
// TODO Auto-generated method stub
position = position.add(velocity.multiply(deltaT));
}
}
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 Canon {
private Point2D position = new Point2D(10, 30);
private int angle = 20;
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.STEELBLUE);
gc.setTransform(new Affine(Transform.rotate(angle, position.getX(), position.getY() + 10)));
gc.fillRect(position.getX(), position.getY() + 6, 40, 12);
gc.restore();
}
public void simulate(double deltaT) {
angle+= 5;
}
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class World {
private double width;
private 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, 10), new Point2D(10, 30), 40);
this.canon = new Canon();
}
public Point2D getCanvasPoint(Point2D in) {
Point2D result = new Point2D(in.getX(), height - in.getY());
return result;
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.LIGHTGREY);
gc.fillRect(0, 0, width, height);
gc.restore();
bullet.draw(gc);
canon.draw(gc);
}
public void simulate(double deltaT) {
this.bullet.simulate(deltaT);
this.canon.simulate(deltaT);
}
}
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