Example usage for javafx.animation AnimationTimer AnimationTimer

List of usage examples for javafx.animation AnimationTimer AnimationTimer

Introduction

In this page you can find the example usage for javafx.animation AnimationTimer AnimationTimer.

Prototype

public AnimationTimer() 

Source Link

Document

Creates a new timer.

Usage

From source file:Main.java

@Override
public void start(Stage stage) {
    Group p = new Group();
    Scene scene = new Scene(p);
    stage.setScene(scene);/*ww w  .ja  v  a2s .c  o m*/
    stage.setWidth(500);
    stage.setHeight(500);
    p.setTranslateX(80);
    p.setTranslateY(80);

    //create a circle with effect
    final Circle circle = new Circle(20, Color.rgb(156, 216, 255));
    circle.setEffect(new Lighting());
    //create a text inside a circle
    final Text text = new Text(i.toString());
    text.setStroke(Color.BLACK);
    //create a layout for circle with text inside
    final StackPane stack = new StackPane();
    stack.getChildren().addAll(circle, text);
    stack.setLayoutX(30);
    stack.setLayoutY(30);

    p.getChildren().add(stack);
    stage.show();

    //create a timeline for moving the circle
    timeline = new Timeline();
    timeline.setCycleCount(Timeline.INDEFINITE);
    timeline.setAutoReverse(true);

    //You can add a specific action when each frame is started.
    timer = new AnimationTimer() {
        @Override
        public void handle(long l) {
            text.setText(i.toString());
            i++;
        }
    };

    //create a keyValue with factory: scaling the circle 2times
    KeyValue keyValueX = new KeyValue(stack.scaleXProperty(), 2);
    KeyValue keyValueY = new KeyValue(stack.scaleYProperty(), 2);

    //create a keyFrame, the keyValue is reached at time 2s
    Duration duration = Duration.millis(2000);
    //one can add a specific action when the keyframe is reached
    EventHandler onFinished = new EventHandler<ActionEvent>() {
        public void handle(ActionEvent t) {
            stack.setTranslateX(java.lang.Math.random() * 200 - 100);
            //reset counter
            i = 0;
        }
    };

    KeyFrame keyFrame = new KeyFrame(duration, onFinished, keyValueX, keyValueY);

    //add the keyframe to the timeline
    timeline.getKeyFrames().add(keyFrame);

    timeline.play();
    timer.start();
}

From source file:eu.mihosoft.fx.tutorials.gravity.SolarSystem.java

/**
 * Initializes the ui frame listener.//from   w  ww . j  a  v  a 2 s.co m
 *
 * @param ode ode to solve
 * @param y state vector (location and velocity)
 * @param m particle masses
 * @param ignoreFlags ignore flags (used for collisions)
 * @param dt time step size
 */
private void initFrameListener(FirstOrderDifferentialEquations ode, double[] y, final double[] m,
        final boolean[] ignoreFlags, double dt) {

    // local time scale (sec per h * h per day * days per year)
    final double localTimeScale = 3600 * 24 * 365;

    // integrator
    FirstOrderIntegrator integrator = new ClassicalRungeKuttaIntegrator(dt * localTimeScale);

    double[] yPrev = new double[y.length];
    double[] interpolatedY = new double[y.length];

    // create frame listener 
    frameListener = new AnimationTimer() {

        @Override
        public void handle(long now) {

            // thanks to http://gafferongames.com/game-physics/fix-your-timestep/
            // measure elapsed time between last and current pulse (frame)
            double frameDuration = (now - lastTimeStamp) / 1e9;
            lastTimeStamp = now;

            // we don't allow frame durations above 2*dt
            if (frameDuration > 2 * dt) {
                frameDuration = 2 * dt;
            }

            // add elapsed time to remaining simulation interval
            remainingSimulationTime += frameDuration;

            // copy current state to prev state
            System.arraycopy(y, 0, yPrev, 0, yPrev.length);

            // simulate remaining interval
            while (remainingSimulationTime >= dt) {

                double scaledT = time * localTimeScale;
                double scaledDT = dt * localTimeScale * timeScale;

                // integrate one step
                try {
                    integrator.integrate(ode, scaledT, y, scaledT + scaledDT, y);

                } catch (Exception ex) {
                    ex.printStackTrace(System.err);
                }
                // remove integrated interval from remaining simulation time
                remainingSimulationTime -= dt;

                // update t
                time = time + dt;
            }

            // interpolate state
            double alpha = remainingSimulationTime / dt;

            // set interpolated state
            for (int i = 0; i < y.length; i++) {
                interpolatedY[i] = y[i] * alpha + yPrev[i] * (1.0 - alpha);
            }

            // update properties for visualization
            updateView(nodes, interpolatedY, m, ignoreFlags);
        }
    };

    // finally, start the framle listener
    frameListener.start();
}

From source file:tw.edu.sju.ee.eea.module.iepe.project.window.IepeRealtimeSpectrumElement.java

private void prepareTimeline() {
    // Every frame to take any data from queue and add to chart
    new AnimationTimer() {
        @Override/*from  w  ww  .  ja  va  2  s .c om*/
        public void handle(long now) {
            addDataToSeries();
        }
    }.start();
}