Skip to content
Snippets Groups Projects
Commit 605858c3 authored by koz01's avatar koz01
Browse files

solution

parent eabbd34d
No related merge requests found
Pipeline #64 failed with stages
in 0 seconds
......@@ -5,6 +5,7 @@ import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
......@@ -18,7 +19,6 @@ public class App extends Application {
launch(args);
}
private Canvas canvas;
private AnimationTimer timer;
@Override
......@@ -26,9 +26,10 @@ public class App extends Application {
try {
//Construct a main window with a canvas.
Group root = new Group();
canvas = new Canvas(800, 400);
Canvas canvas = new Canvas(800, 400);
root.getChildren().add(canvas);
Scene scene = new Scene(root, 800, 400);
scene.setFill(Color.BLANCHEDALMOND);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.resizableProperty().set(false);
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
public class Bullet {
private final World world;
private Point2D position;
private Point2D velocity;
private final double size;
public Bullet(World world, Point2D position, Point2D velocity, double size) {
this.world = world;
this.position = position;
this.velocity = velocity;
this.size = size;
}
public void simulate(double deltaT) {
position = position.add(velocity.multiply(deltaT));
position = new Point2D((position.getX() + world.getWidth()) % world.getWidth(), (position.getY() + world.getHeight()) % world.getHeight());
}
public void draw(GraphicsContext gc) {
gc.setFill(Color.RED);
Point2D p = world.transform2Canvas(position);
gc.fillOval(p.getX(), p.getY(), size, size);
}
protected World getWorld() {
return world;
}
protected Point2D getPosition() {
return position;
}
protected double getSize() {
return size;
}
//simulate
//draw
}
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
public class BulletAnimated extends Bullet{
public BulletAnimated(World world, Point2D position, Point2D velocity, double size) {
super(world, position, velocity, size);
}
private Image image = new Image(getClass().getResourceAsStream("fireball-transparent.gif"));
//gc.drawImage(image, width, height)
@Override
public void draw(GraphicsContext gc) {
Point2D p = getWorld().transform2Canvas(getPosition());
gc.drawImage(image,p.getX(), p.getY(), getSize(), getSize());
}
}
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 World world;
private final Point2D position;
private double angle;
private int speed = 10;
public Cannon(World world, Point2D position, double angle) {
this.world = world;
this.position = position;
this.angle = angle;
}
public void draw(GraphicsContext gc) {
gc.save();
gc.setFill(Color.LIGHTSTEELBLUE);
Point2D p = world.transform2Canvas(position);
gc.setTransform(new Affine(Transform.rotate(-angle, p.getX(), p.getY())));
gc.fillRect(p.getX(), p.getY(), 100, 20);
gc.restore();
}
public void simulate(double deltaT) {
angle = angle + speed*deltaT;
if (angle > 90) {
angle = 180 - angle;
speed *= -1;
} else if (angle < 0){
angle = -angle;
speed *= -1;
}
}
}
......@@ -8,9 +8,11 @@ public class DrawingThread extends AnimationTimer {
private final GraphicsContext gc;
private long lastTime;
private final World world;
public DrawingThread(Canvas canvas) {
this.gc = canvas.getGraphicsContext2D();
this.world = new World(canvas.getWidth(), canvas.getHeight());
}
/**
......@@ -21,8 +23,10 @@ public class DrawingThread extends AnimationTimer {
if (lastTime > 0) {
double deltaT = (now - lastTime) / 1e9;
// call simulate on world
world.simulate(deltaT);
}
//call draw on world
world.draw(gc);
lastTime= now;
}
......
package lab;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
public class World {
private final double width;
private final double height;
private final Bullet bullet;
private final Cannon cannon;
public World(double width, double height) {
this.width = width;
this.height = height;
this.bullet = new BulletAnimated(this, new Point2D(50, 50), new Point2D(100, 100), 30);
this.cannon = new Cannon(this, new Point2D(50, 50), 45);
}
public void simulate(double deltaT) {
this.bullet.simulate(deltaT);
this.cannon.simulate(deltaT);
}
public void draw(GraphicsContext gc) {
gc.clearRect(0, 0, width, height);
this.bullet.draw(gc);
this.cannon.draw(gc);
}
public Point2D transform2Canvas(Point2D p) {
return new Point2D(p.getX(), height - p.getY());
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
}
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