Skip to content
Snippets Groups Projects
Commit 57e71213 authored by kaller01's avatar kaller01
Browse files

init

parent 0a13947a
No related branches found
No related tags found
No related merge requests found
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);
}
}
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