javafx.animation.AnimationTimer Java Examples

The following examples show how to use javafx.animation.AnimationTimer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From FXTutorials with MIT License 8 votes vote down vote up
@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(appRoot);
    scene.setOnKeyPressed(event -> keys.put(event.getCode(), true));
    scene.setOnKeyReleased(event -> keys.put(event.getCode(), false));
    primaryStage.setTitle("Tutorial 14 Platformer");
    primaryStage.setScene(scene);
    primaryStage.show();

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            update();
        }
    };
    timer.start();
}
 
Example #2
Source File: Smoke.java    From tilesfx with Apache License 2.0 8 votes vote down vote up
public Smoke() {
    running       = false;
    ctx           = getGraphicsContext2D();
    width         = getWidth();
    height        = getHeight();
    particles     = new CopyOnWriteArrayList<>();
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + GENERATION_RATE) {
                if (running && particles.size() < NO_OF_PARTICLES) { particles.add(new ImageParticle(IMAGE, width, height)); }
                if (particles.isEmpty()) timer.stop();
                lastTimerCall = NOW;
            }
            draw();
        }
    };
    setMouseTransparent(true);
    registerListeners();
}
 
Example #3
Source File: Game.java    From CrazyAlpha with GNU General Public License v2.0 7 votes vote down vote up
public void exit() {
    new AnimationTimer() {
        long time = System.currentTimeMillis();

        @Override
        public void handle(long l) {
            if (System.currentTimeMillis() - time < 550) {
                try {
                    Thread.sleep(10);
                } catch (Exception ex) {
                    logger.error(ex.getMessage());
                }
            } else
                System.exit(0);
        }
    }.start();
}
 
Example #4
Source File: Main.java    From FXTutorials with MIT License 7 votes vote down vote up
@Override
public void start(Stage primaryStage) throws Exception {
    initContent();

    Scene scene = new Scene(appRoot);
    scene.setOnKeyPressed(event -> keys.put(event.getCode(), true));
    scene.setOnKeyReleased(event -> keys.put(event.getCode(), false));
    primaryStage.setTitle("Tutorial 14 Platformer");
    primaryStage.setScene(scene);
    primaryStage.show();

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            if (running) {
                update();
            }

            if (dialogEvent) {
                dialogEvent = false;
                keys.keySet().forEach(key -> keys.put(key, false));

                GameDialog dialog = new GameDialog();
                dialog.setOnCloseRequest(event -> {
                    if (dialog.isCorrect()) {
                        System.out.println("Correct");
                    }
                    else {
                        System.out.println("Wrong");
                    }

                    running = true;
                });
                dialog.open();
            }
        }
    };
    timer.start();
}
 
Example #5
Source File: AlgorithmApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    Pane root = new Pane();

    Canvas canvas = new Canvas(W, H);
    g = canvas.getGraphicsContext2D();
    root.getChildren().add(canvas);

    for (int y = 0; y < grid[0].length; y++) {
        for (int x = 0; x < grid.length; x++) {
            grid[x][y] = new Tile(x, y);
        }
    }

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            update();
        }
    };
    timer.start();

    algorithm = AlgorithmFactory.breadthFirst();
    algorithmThread.scheduleAtFixedRate(this::algorithmUpdate, 0, 1, TimeUnit.MILLISECONDS);

    return root;
}
 
Example #6
Source File: EventStreams.java    From ReactFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Returns an event stream that emits a timestamp of the current frame in
 * nanoseconds on every frame. The timestamp has the same meaning as the
 * argument of the {@link AnimationTimer#handle(long)} method.
 */
public static EventStream<Long> animationTicks() {
    return new EventStreamBase<Long>() {
        private final AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                emit(now);
            }
        };

        @Override
        protected Subscription observeInputs() {
            timer.start();
            return timer::stop;
        }
    };
}
 
Example #7
Source File: Led.java    From JFX8CustomControls with Apache License 2.0 6 votes vote down vote up
public Led() {
    getStylesheets().add(getClass().getResource("led.css").toExternalForm());
    getStyleClass().add("led");
            
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {                    
                setOn(!isOn());
                lastTimerCall = NOW;
            }
        }
    };
    init();
    initGraphics();
    registerListeners();
}
 
Example #8
Source File: LedBargraphSkin.java    From Enzo with Apache License 2.0 6 votes vote down vote up
public LedBargraphSkin(final LedBargraph CONTROL) {
    super(CONTROL);
    ledList         = new ArrayList<>(getSkinnable().getNoOfLeds());
    stepSize        = new SimpleDoubleProperty(1.0 / getSkinnable().getNoOfLeds());
    lastTimerCall   = 0l;
    peakLedIndex    = 0;
    timer           = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + PEAK_TIMEOUT) {
                ledList.get(peakLedIndex).setOn(false);
                peakLedIndex = 0;
                timer.stop();
            }
        }
    };

    init();
    initGraphics();
    registerListeners();
}
 
Example #9
Source File: VuMeterSkin.java    From Enzo with Apache License 2.0 6 votes vote down vote up
public VuMeterSkin(final VuMeter CONTROL) {
    super(CONTROL);
    active        = false;
    lastTimerCall = 0l;
    stepSize      = new SimpleDoubleProperty((getSkinnable().getMaxValue() - getSkinnable().getMinValue()) / getSkinnable().getNoOfLeds());
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + PEAK_TIMEOUT) {
                leds.get(peakLedIndex).getStyleClass().remove("led-peak");
                peakLedIndex = Orientation.HORIZONTAL == getSkinnable().getOrientation() ? 0 : leds.size() - 1;
                timer.stop();
            }
        }
    };
    init();
    initGraphics();
    registerListeners();
}
 
Example #10
Source File: Led.java    From Enzo with Apache License 2.0 6 votes vote down vote up
public Led() {
    toggle        = false;
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {
                toggle ^= true;
                setOn(toggle);
                lastTimerCall = NOW;
            }
        }
    };
    init();
    initGraphics();
    registerListeners();
}
 
Example #11
Source File: AsteroidsApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    root = new Pane();
    root.setPrefSize(600, 600);

    player = new Player();
    player.setVelocity(new Point2D(1, 0));
    addGameObject(player, 300, 300);

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            onUpdate();
        }
    };
    timer.start();

    return root;
}
 
Example #12
Source File: DrawingApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    Pane root = new Pane();
    root.setPrefSize(800, 600);

    Canvas canvas = new Canvas(800, 600);
    g = canvas.getGraphicsContext2D();

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            t += 0.017;
            draw();
        }
    };
    timer.start();

    root.getChildren().add(canvas);
    return root;
}
 
Example #13
Source File: Main.java    From FXTutorials with MIT License 6 votes vote down vote up
@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(createContent());
    scene.setOnMouseClicked(event -> {
        bullet.setTarget(event.getSceneX(), event.getSceneY());
    });

    primaryStage.setTitle("Tutorial");
    primaryStage.setScene(scene);
    primaryStage.show();
    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            bullet.move();
        }
    };
    timer.start();
}
 
Example #14
Source File: ParticlesClockApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {

        Pane root = new Pane();
        root.setPrefSize(800, 600);

        Canvas canvas = new Canvas(800, 600);
        g = canvas.getGraphicsContext2D();
        g.setFill(Color.BLUE);

        root.getChildren().add(canvas);

        populateDigits();
        populateParticles();

        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                onUpdate();
            }
        };
        timer.start();

        return root;
    }
 
Example #15
Source File: SpaceInvadersApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    root.setPrefSize(600, 800);

    root.getChildren().add(player);

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            update();
        }
    };

    timer.start();

    nextLevel();

    return root;
}
 
Example #16
Source File: FroggerApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    root = new Pane();
    root.setPrefSize(800, 600);

    frog = initFrog();

    root.getChildren().add(frog);

    timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            onUpdate();
        }
    };
    timer.start();

    return root;
}
 
Example #17
Source File: SimplePerformanceMeter.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
public SimplePerformanceMeter(Scene scene, long updateDuration) {
    if (scene == null) {
        throw new IllegalArgumentException("scene must not be null");
    }
    this.scene = scene;
    this.updateDuration = Math.max(MIN_UPDATE_PERIOD, Math.min(updateDuration, MAX_UPDATE_PERIOD));
    fxPerformanceTracker = PerformanceTracker.getSceneTracker(scene); // keep for the future in case this becomes public

    animationTimer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            pulseCounter.getAndIncrement();
        }
    };

    Field field1 = null;
    Field field2 = null;
    try {
        field1 = Node.class.getDeclaredField("dirtyBits");
        field1.setAccessible(true);
        field2 = Scene.class.getDeclaredField("dirtyNodesSize");
        field2.setAccessible(true);
    } catch (SecurityException | NoSuchFieldException e) {
        LOGGER.atError().setCause(e).log("cannot access scene root's dirtyBits field");
    }
    dirtyRootBits = field1;
    dirtyNodesSize = field2;

    registerListener(); // NOPMD
}
 
Example #18
Source File: Led.java    From Enzo with Apache License 2.0 5 votes vote down vote up
public Led() {
    getStyleClass().add("led");
    toggle        = false;
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {
                toggle ^= true;
                setOn(toggle);
                lastTimerCall = NOW;
            }
        }
    };
}
 
Example #19
Source File: Skybox.java    From FXyzLib with GNU General Public License v3.0 5 votes vote down vote up
private void startTimer(){
    timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            Transform ct = (camera != null) ? camera.getLocalToSceneTransform() : null;
            if(ct != null){
                affine.setTx(ct.getTx());
                affine.setTy(ct.getTy());
                affine.setTz(ct.getTz());
            }
        }
    };
    timer.start();
}
 
Example #20
Source File: CameraController.java    From FXyzLib with GNU General Public License v3.0 5 votes vote down vote up
public CameraController(boolean enableTransforms, AnimationPreference movementType) {
    enable = enableTransforms;
    animPref = movementType;
    switch (animPref) {
        case TIMELINE:
            timeline = new Timeline();
            timeline.setCycleCount(Animation.INDEFINITE);
            break;
        case TIMER:
            timer = new AnimationTimer() {
                @Override
                public void handle(long l) {
                    if (enable) {
                        initialize();
                        enable = false;
                    }
                    update();
                }
            };
            break;
        case TRANSITION:
            transition = new Transition() {
                {setCycleDuration(Duration.seconds(1));}
                @Override
                protected void interpolate(double frac) {
                    updateTransition(frac);
                }
            };
            transition.setCycleCount(Animation.INDEFINITE);
            break;
        case ANIMATION:
            
            break;
    }

}
 
Example #21
Source File: GUIProfiler.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void init() {
    AnimationTimer fpsTimer = new AnimationTimer() {
        @Override
        public void handle(long l) {
            long elapsed = (System.currentTimeMillis() - lastFPSTime);
            if (elapsed > 50)
                log.trace("Profiler: last frame used {}ms", elapsed);

            lastFPSTime = System.currentTimeMillis();
        }
    };
    fpsTimer.start();
}
 
Example #22
Source File: FinanceUI.java    From StockPrediction with Apache License 2.0 5 votes vote down vote up
private void prepareTimeline() {
    new AnimationTimer() {
        @Override
        public void handle(long now) {
            addDataToSeries();
        }
    }.start();
}
 
Example #23
Source File: Led.java    From Enzo with Apache License 2.0 5 votes vote down vote up
public Led(final Color LED_COLOR, final boolean FRAME_VISIBLE, final long INTERVAL, final boolean BLINKING) {
    getStylesheets().add(Led.class.getResource("led.css").toExternalForm());
    getStyleClass().add("led");
    _ledColor     = LED_COLOR;
    _frameVisible = FRAME_VISIBLE;
    _interval     = INTERVAL;
    _blinking     = BLINKING;
    toggle        = false;
    lastTimerCall = System.nanoTime();
    timer = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {
                toggle ^= true;
                setOn(toggle);
                lastTimerCall = NOW;
            }
        }
    };
    init();
    initGraphics();
    registerListeners();
    if (_blinking) {
        timer.start();
    } else {
        timer.stop();
    }
}
 
Example #24
Source File: Example4K.java    From Introduction-to-JavaFX-for-Game-Development with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void start(Stage mainStage)
{
    mainStage.setTitle("Event Handling");

    Group root = new Group();
    mainScene = new Scene(root);
    mainStage.setScene(mainScene);

    Canvas canvas = new Canvas(WIDTH, HEIGHT);
    root.getChildren().add(canvas);

    prepareActionHandlers();

    graphicsContext = canvas.getGraphicsContext2D();

    loadGraphics();

    /**
     * Main "game" loop
     */
    new AnimationTimer()
    {
        public void handle(long currentNanoTime)
        {
            tickAndRender();
        }
    }.start();

    mainStage.show();
}
 
Example #25
Source File: FinanceUI.java    From StockInference-Spark with Apache License 2.0 5 votes vote down vote up
private void prepareTimeline() {
    new AnimationTimer() {
        @Override
        public void handle(long now) {
            addDataToSeries();
        }
    }.start();
}
 
Example #26
Source File: Snake3dApp.java    From FXTutorials with MIT License 5 votes vote down vote up
private Scene createScene() {
    Cube cube = new Cube(Color.BLUE);
    snake.getChildren().add(cube);

    moveFood();

    root.getChildren().addAll(snake, food);

    Scene scene = new Scene(root, 1280, 720, true);

    PerspectiveCamera camera = new PerspectiveCamera(true);
    camera.getTransforms().addAll(new Translate(0, -20, -20), new Rotate(-45, Rotate.X_AXIS));

    scene.setCamera(camera);

    timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            t += 0.016;

            if (t > 0.1) {
                onUpdate();
                t = 0;
            }
        }
    };

    timer.start();

    return scene;
}
 
Example #27
Source File: Led.java    From JFX8CustomControls with Apache License 2.0 5 votes vote down vote up
public Led() {
    getStyleClass().add("led");        
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {
                setOn(!isOn());
                lastTimerCall = NOW;
            }
        }
    };
}
 
Example #28
Source File: RadarChartTest.java    From charts with Apache License 2.0 5 votes vote down vote up
@Override public void init() {
    List<YChartItem> item1 = new ArrayList<>(ELEMENTS);
    List<YChartItem> item2 = new ArrayList<>(ELEMENTS);
    List<YChartItem> item3 = new ArrayList<>(ELEMENTS);
    for (int i = 0 ; i < ELEMENTS ; i++) {
        YChartItem dataPoint;

        dataPoint = new YChartItem(RND.nextDouble() * 100, "P" + i);
        item1.add(dataPoint);

        dataPoint = new YChartItem(RND.nextDouble() * 100, "P" + i);
        item2.add(dataPoint);

        dataPoint = new YChartItem(RND.nextDouble() * 100, "P" + i);
        item3.add(dataPoint);
    }

    series1 = new YSeries(item3, CHART_TYPE, new RadialGradient(0, 0, 0, 0, 1, true, CycleMethod.NO_CYCLE, new Stop(0.0, Color.rgb(0, 255, 255, 0.25)), new Stop(0.5, Color.rgb(255, 255, 0, 0.5)), new Stop(1.0, Color.rgb(255, 0, 255, 0.75))), Color.TRANSPARENT);
    series2 = new YSeries(item1, CHART_TYPE, new RadialGradient(0, 0, 0, 0, 1, true, CycleMethod.NO_CYCLE, new Stop(0.0, Color.rgb(255, 0, 0, 0.25)), new Stop(0.5, Color.rgb(255, 255, 0, 0.5)), new Stop(1.0, Color.rgb(0, 200, 0, 0.75))), Color.TRANSPARENT);
    series3 = new YSeries(item2, CHART_TYPE, new RadialGradient(0, 0, 0, 0, 1, true, CycleMethod.NO_CYCLE, new Stop(0.0, Color.rgb(0, 255, 255, 0.25)), new Stop(0.5, Color.rgb(0, 255, 255, 0.5)), new Stop(1.0, Color.rgb(0, 0, 255, 0.75))), Color.TRANSPARENT);
    series2.setWithWrapping(true);
    chart   = new YChart(new YPane(series1, series2, series3));
    chart.setPrefSize(600, 600);

    timeline      = new Timeline();
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long now) {
            if (now > lastTimerCall + INTERVAL) {
                animateData();
                long delta = System.nanoTime() - now;
                timeline.play();
                lastTimerCall = now + delta;
            }
        }
    };

    registerListener();
}
 
Example #29
Source File: Led.java    From JFX8CustomControls with Apache License 2.0 5 votes vote down vote up
public Led() {
    getStyleClass().add("led");        
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {                    
                setOn(!isOn());
                lastTimerCall = NOW;
            }
        }
    };
}
 
Example #30
Source File: Led.java    From JFX8CustomControls with Apache License 2.0 5 votes vote down vote up
public Led() {        
    lastTimerCall = System.nanoTime();
    timer         = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {                  
                setOn(!isOn());
                lastTimerCall = NOW;
            }
        }
    };
    init();
    initGraphics();
    registerListeners();
}