Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.animation;
027
028import com.sun.scenario.ToolkitAccessor;
029import com.sun.scenario.animation.AbstractMasterTimer;
030
031/**
032 * The class {@code AnimationTimer} allows to create a timer, that is called in
033 * each frame while it is active.
034 * 
035 * An extending class has to override the method {@link #handle(long)} which
036 * will be called in every frame.
037 * 
038 * The methods {@link AnimationTimer#start()} and {@link #stop()} allow to start
039 * and stop the timer.
040 * 
041 * 
042 */
043public abstract class AnimationTimer {
044    
045    private final AbstractMasterTimer timer;
046    private boolean active;
047    
048    public AnimationTimer() {
049        timer = ToolkitAccessor.getMasterTimer();
050    }
051    
052    // For testing only
053    AnimationTimer(AbstractMasterTimer timer) {
054        this.timer = timer;
055    }
056    
057    /**
058     * This method needs to be overridden by extending classes. It is going to
059     * be called in every frame while the {@code AnimationTimer} is active.
060     * 
061     * @param now
062     *            The timestamp of the current frame given in nanoseconds. This
063     *            value will be the same for all {@code AnimationTimers} called
064     *            during one frame.
065     */
066    public abstract void handle(long now);
067
068    /**
069     * Starts the {@code AnimationTimers}. Once it is started, the
070     * {@link #handle(long)} method of this {@code AnimationTimers} will be
071     * called in every frame.
072     * 
073     * The {@code AnimationTimers} can be stopped by calling {@link #stop()}.
074     */
075    public void start() {
076        if (!active) {
077            timer.addAnimationTimer(this);
078            active = true;
079        }
080    }
081
082    /**
083     * Stops the {@code AnimationTimers}. It can be activated again by calling
084     * {@link #start()}.
085     */
086    public void stop() {
087        if (active) {
088            timer.removeAnimationTimer(this);
089            active = false;
090        }
091    }
092}