Moving Text and Image

To make the text move and load an image using JavaFX is very simple. The benefit of JavaFX is, it can load Photoshop extension images. I will also talk a little bit about working with text Fonts and Size here. We will need to import this packages

The first three packages should be familiar, we've used it in the previous section. The last two packages, we use it for animations.

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.*;


import javafx.animation.transition.TranslateTransition;
import javafx.animation.*;

Now, lets observe an example so it gives us a clear idea on how it works
 

Moving Text Example



Click here to download the example


In JavaFX, using a colon " : ", it means assign something to the variable. Notice, we use var to declare a variable that holds a text and TranslateTransition. A variable can hold pretty much anything, value, string or characters. Inside TranslateTransition we create the duration of the text, which is the speed of the text, we bind the text we want to animate, the position the text is moving, how it moves, and the repetition time. Look closely  how we assign Font to font with size and types. In the end we have to use the play method to play the animation.

 

Moving Image

To make an image to move by itself, the idea is the same as making a text move by itself. The following example makes an image to move in the Y direction linearly.


Example




Click here to download the example

 

Note: We always need to have the play() method, if we forgot to put it in the animation will not play.
 

 

  <JavaFX><Home>