/** * Handlers check for keypresses left, right, up, down arrow keys If left is pressed, add value * of-5 to left move (decrease X to move left) right is pressed, add 5 to right move (increase X * to move up) up is pressed, add -5 to up (decrease Y to move up) down is pressed, add 5 to down * (increase Y to move down) * * @param scene Scene */ public static void addMovementHandlers(Scene scene) { scene.addEventHandler( KeyEvent.KEY_PRESSED, k -> { if (k.getCode() == KeyCode.LEFT) { Configurations.getLoopService().setL(-5); } if (k.getCode() == KeyCode.RIGHT) { Configurations.getLoopService().setR(5); } if (k.getCode() == KeyCode.UP) { Configurations.getLoopService().setU(-5); } if (k.getCode() == KeyCode.DOWN) { Configurations.getLoopService().setD(5); } }); scene.addEventHandler( KeyEvent.KEY_RELEASED, k -> { if (k.getCode() == KeyCode.LEFT) { Configurations.getLoopService().setL(0); } if (k.getCode() == KeyCode.RIGHT) { Configurations.getLoopService().setR(0); } if (k.getCode() == KeyCode.UP) { Configurations.getLoopService().setU(0); } if (k.getCode() == KeyCode.DOWN) { Configurations.getLoopService().setD(0); } }); }
@Override public void start(Stage stage) throws Exception { this.primaryStage = stage; primaryStage.setTitle("Matrix Digital Rain"); Group root = new Group(); Scene scene = new Scene(root, 1024, 768); scene.addEventHandler( KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent keyEvent) { if (keyEvent.getCode() == KeyCode.F) { primaryStage.setFullScreen(!primaryStage.isFullScreen()); } if (keyEvent.isControlDown() && keyEvent.getCode() == KeyCode.Q) { primaryStage.close(); } } }); Canvas canvas = new Canvas(); canvas.widthProperty().bind(primaryStage.widthProperty()); canvas.heightProperty().bind(primaryStage.heightProperty()); final GraphicsContext gc = canvas.getGraphicsContext2D(); gc.setFont(font); setupSimulation(); new AnimationTimer() { @Override public void handle(long now) { update(now); gc.clearRect(0, 0, primaryStage.getWidth(), primaryStage.getHeight()); gc.setFill(Color.rgb(0, 0, 1)); gc.fillRect(0, 0, primaryStage.getWidth(), primaryStage.getHeight()); int y = 0; int nbGlyphsPerRow = getNbGlyphsPerRow(); int nbGlyphsPerColumn = getNbGlyphsPerColumn(); for (int i = 0; i < nbGlyphsPerRow * nbGlyphsPerColumn; ++i) { gc.setFill(Color.rgb(0, path[i], 0)); String text = String.valueOf(data[i]); gc.fillText(text, (i % nbGlyphsPerRow) * 12 + 1, y + 13); if (i % nbGlyphsPerRow == nbGlyphsPerRow - 1) { y += 12; } } } }.start(); root.getChildren().add(canvas); primaryStage.setScene(scene); primaryStage.show(); }
/** * Registers this {@link EventSynchronizer} for its event type. After registration, you can wait * for the event processing by calling {@link #await()}. */ public void register() { if (isRegistered) { throw new IllegalStateException("already registered"); } latch = new CountDownLatch(1); handler = new EventHandler<T>() { @Override public void handle(T event) { latch.countDown(); } }; if (scene == null) { node.addEventHandler(type, handler); } else { scene.addEventHandler(type, handler); } isRegistered = true; }