Javafx example-Use of timeline and animation

Source: Internet
Author: User
Tags border color

The work was very busy in the past two days. Due to the final release of a project on the Android tablet, I had to work overtime for more than 11 o'clock the night before and work overtime for more than 4 o'clock last night. Therefore, the home page of SourceForge is also a part. However, due to the final release project, it is easy today to write several javafx examples.


The above is a simple example.

Demo address for example: http://wjfxgame.sourceforge.net/examples/exp1/AniTest.html

Click Start to start the animation.


It mainly applies timeline and animation in javafx.

Timeline is used in many places in previous blog posts. That is, a timeline, and then an animation is formed by adding a key frame.


The animation is mainly used to animation the node. The animation in javafx is in the javafx. animation package. Animation has two sub-classes: timeline and transition. That is to say, according to the hierarchical relationship in the API, timeline is a type of animation.


Of course, we don't have to worry about timeline and animation in the title.


Transition has 10 sub-classes: fadetransition, filltransition, paralleltransition, pathtransition, transform, rotatetransition, scaletransition, sequentialtransition, stroketransition, and translatetransition.


In fact, although there are many sub-classes, the basic effects of animation can be determined based on the naming prefix. It is basically a change of node attributes, such as border color, fill color, transparency, scaling, rotation, pan, move by path and pause.


Only paralleltransition is a parallel animation (a series of animations are simultaneously executed), and sequentialtransition is a sequential animation (a series of animations are executed in order ).


Let's take a look at the source code:

Import javafx. animation. *; import javafx. event. actionevent; import javafx. event. eventhandler; import javafx. scene. control. togglebutton; import javafx. scene. control. togglegroup; import javafx. scene. effect. bloom; import javafx. scene. layout. hbox; import javafx. scene. layout. pane; import javafx. scene. paint. color; import javafx. scene. shape. *; import javafx. util. duration;/***** @ author wing */public class testpane Extends pane {private paralleltransition manimlist; private timeline; private hbox; private togglebutton start, pause, stop; private togglegroup btngroup; private double duration = 200; Public testpane () {btngroup = new togglegroup (); Start = new togglebutton ("START"); start. settogglegroup (btngroup); start. setonaction (New eventhandler <actionevent> () {@ override public void handle (AC Tionevent arg0) {timeline. play (); checkuistate () ;}}); pause = new togglebutton ("pause"); pause. settogglegroup (btngroup); pause. setonaction (New eventhandler <actionevent> () {@ override public void handle (actionevent arg0) {timeline. pause (); checkuistate () ;}}); stop = new togglebutton ("stop"); stop. settogglegroup (btngroup); stop. setonaction (New eventhandler <actionevent> () {@ override public voi D handle (actionevent arg0) {timeline. stop (); checkuistate () ;}}); hbox = new hbox (10); hbox. getchildren (). addall (START, pause, stop); hbox. settranslatex (anitest. width-200)/2); hbox. settranslatey (20); getchildren (). add (hbox); timeline = new timeline (); timeline. setcyclecount (timeline. indefinite); keyframe = new keyframe (duration. millis (duration), new eventhandler <actionevent> () {@ o Verride public void handle (actionevent event) {Createobject () ;}}); timeline. getkeyframes (). add (keyframe);}/*** check the status of the Start pause stop buttons */Public void checkuistate () {start. setdisable (false); pause. setdisable (false); stop. setdisable (false); Switch (timeline. getstatus () {Case running: Start. setdisable (true); break; Case paused: pause. setdisable (true); break; Case stopped: Stop. setdisable (tru E); break ;}}/*** create an object and execute an animation. Here, a paralleltransition is created, and a translatetransition with random up/down translation is added to it, the transparency gradually changes to the fadetransition of 0 and the scaletransition of 0.2 times. Bind the created object to the paralleltransition that combines the three animations, and then execute paralleltransition. */Public void Createobject () {double width = math. max (50, math. random () * 200); double height = math. max (50, math. random () * 200); Double X = math. min (math. random () * anitest. width, anitest. width-width); Double Y = math. max (math. random () * (anitest. height-100), 100); double dx = math. random () * 50; double DY = math. random () * 50; final shape = new circle (X, Y, width/2); shape. seteffe CT (new bloom (50); shape. setfill (color. RGB (INT) (math. random () * 255), (INT) (math. random () * 255), (INT) (math. random () * 255); getchildren (). add (SHAPE); manimlist = new paralleltransition (shape, translatetransitionbuilder. create (). byx (math. random ()> 0.5? DX:-dx). byY (math. Random ()> 0.5? DY:-dy ). duration (duration. millis (1000 )). build (), fadetransitionbuilder. create (). tovalue (0 ). duration (duration. millis (1000 )). build (), scaletransitionbuilder. create (). byx (0.2 ). byY (0.2 ). duration (duration. millis (800 )). build (); manimlist. play (); manimlist. setonfinished (New eventhandler <actionevent> () {@ override public void handle (actionevent arg0) {getchildren (). remove (SHAPE );}});}}


At the end of the animation, remove the newly created object.


The transition here is created using the respective builder.


As to why pane will be inherited here, I will add these examples to the site on the SourceForge homepage and use them in that app.


The following is the main class:

Import javafx. application. application; import javafx. scene. scene; import javafx. scene. layout. stackpane; import javafx. scene. paint. color; import javafx. stage. stage;/*** @ author wing * 2012/8/30 */public class anitest extends application {public static final int width = 800; public static final int Height = 600; public static void main (string [] ARGs) {launch (ARGs) ;}@ override public void start (stage primarystage) {testpane mpane = new testpane (); stackpane root = new stackpane (); root. getchildren (). add (mpane); scene = new scene (root, width, height); scene. setfill (color. black); primarystage. setscene (scene); primarystage. settitle ("javafx example -- Use of timeline and Animation"); primarystage. show ();}}

The main class will not be explained, and it is very basic content.

Reprinted please indicate the source: http://blog.csdn.net/ml3947/

---------------------------------------------------------

I tried to embed the applet in the blog post. After trying for half a day, I didn't succeed, but I saw a blog that I 've been paying attention. Strange. Prepare and check again.


So I gave the demo address directly. If javafx is not installed in the system, you will be prompted to install the new Java version. Because jre7 already contains javafx.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.