Skip to content
Snippets Groups Projects
Commit 0a13947a authored by kaller01's avatar kaller01
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
App.java 0 → 100644
import java.util.HashSet;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class App {
private Stage primaryStage;
private Canvas canvas;
// These should probably not be here, just for example
private int x = 0;
private int xSpeed = 3;
public App(Stage stage) {
primaryStage = stage;
Group root = new Group(); // Create root element for scene, ex group
Scene scene = new Scene(root); // Create the scene with root element
canvas = new Canvas(Main.WIDTH, Main.HEIGHT);
root.getChildren().add(canvas);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* This method should be called for every frame
*
* @param dt the passed time since last drawn frame
*/
public void update(double dt) {
GraphicsContext gc = canvas.getGraphicsContext2D();
// clear backgroud, try removing this line.
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.fill();
gc.setFill(Color.GREEN);
gc.fillRect(x, 0, 100, 100);
x += xSpeed;
}
/**
* Update the currently active keys
* @param currentlyActiveKeys
*/
public void update(HashSet<String> currentlyActiveKeys) {
if (currentlyActiveKeys.contains("SPACE"))
xSpeed = -xSpeed;
}
public Scene getScene() {
return primaryStage.getScene();
}
}
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.HashSet;
public class Main extends Application {
public static final double WIDTH = 1600;
public static final double HEIGHT = 800;
public static final int FRAMES_PER_SECOND = 60;
private static final int MILLISECOND_DELAY = 1000 / FRAMES_PER_SECOND;
private static final double SECOND_DELAY = 1.0 / FRAMES_PER_SECOND;
private HashSet<String> currentlyActiveKeys = new HashSet<>();
private App app;
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("JavaFX canvas");
app = new App(primaryStage);
// timeline and fps
KeyFrame frame = new KeyFrame(Duration.millis(MILLISECOND_DELAY), e -> {
app.update(SECOND_DELAY);
});
Timeline animation = new Timeline();
animation.setCycleCount(Timeline.INDEFINITE);
animation.getKeyFrames().add(frame);
animation.play();
// Listeners
keyListener(app.getScene());
}
public void keyListener(Scene scene) {
scene.setOnKeyPressed(event -> {
String codeString = event.getCode().toString();
if (!currentlyActiveKeys.contains(codeString)) {
currentlyActiveKeys.add(codeString);
System.out.println(currentlyActiveKeys);
app.update(currentlyActiveKeys);
}
});
scene.setOnKeyReleased(event -> {
currentlyActiveKeys.remove(event.getCode().toString());
app.update(currentlyActiveKeys);
});
}
public static void main(String[] args) {
launch(args);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment