Use the steps in this section to create your JavaFX project, which will contain the source file for your first JavaFX application. Note that the screen captures used in this document were taken on a Windows XP system.
Figure 1: Selecting JavaFX Project Wizard
Figure 2: Anonymous Usage Data Collection Information
FirstJavaFXApp
for the Project name, as shown in the next image.
Figure 3: Creating a JavaFX Project Wizard
JAVAFX_HOME
variable. Follow the prompts to set this classpath variable. Make sure that you click Folder in the Edit Variable Entry dialog box and navigate to the location of the JavaFX SDK installation folder, for example, javafx-sdk1.2.
After that location is set, continue with the next step below.
Figure 4: FirstJavaFXApp Project Opened in Package Explorer
Main.fx
Filegstutorial
in the Name text field and click Finish.Figure 5: Creating a New Java Package
Main.fx
source file. Main
in the Name text field and click Finish. Main.fx
file is opened in the source editor area. Notice that the comment block for the author information is initially folded, as shown in Figure 6. Click the
Figure 6: Main.fx
File Created
Stage
object to the source editor, as shown in Figure 7.
Figure 7: Adding the Stage
Object
Stage
, edit the title
, width
, and height
variables, as shown in Figure 8.
Figure 8: Modifying Stage Template
Stage
and Scene
. The import
statements for the Stage
and Scene
packages were automatically added when the code snippet was inserted. These object literals represent key concepts within the JavaFX application and are described in the following table.Object Literal | Description |
---|---|
Stage |
The top-level container window required to display any visible JavaFX objects. The default instance variables title , width , and height define the text that appears on the window's top border and the window's height and width. The scene variable defines an instance of the Scene object literal, which sets the area in which you can place the JavaFX objects. |
Scene |
Similar to a drawing surface for the graphical content of your application. The scene instance variable has a content variable that is used to hold JavaFX graphical elements and defines the graphical content of your application. The instance variables, width and height , define the width and height of the content area. For more information about the Scene class, see Presenting UI Objects in a Graphical Scene, a lesson in Building GUI Applications With JavaFX. |
Note: For more information, see Using Objects, a lesson in Learning the JavaFX Script Programming Language.
Scene
object to specify the text that you want to appear in the application. You also need to add the import statement for the TextAlignment
class.import
statements were defined. import javafx.scene.text.*;
content
variable as shown in the following code example. You can copy the lines in bold and paste them into the editor to replace the current content
code block. Stage { title: "First JavaFX App" scene: Scene { width: 250 height: 250 content: [ Text { font: Font { size: 22 } x: 20, y: 90 textAlignment: TextAlignment.CENTER content:"Welcome to /nJavaFX World" } //Text ] // content } // Scene } // Stage
Circle
object from which you create the sphere by expanding the Basic Shapes
section in the Snippets view and dragging the Circle code snippet in the line above the Text
code block, as shown in the following figure.
Figure 9: Dragging the Circle Code Snippet From the Snippets Window
Circle
, modify the circle's radius
variable to have the value of 90
and click Insert.Main.fx
source file, along with the necessary import statements for the Circle
and Color
classes APIs.Circle
to enable the Circle
class API to be displayed in a pop-up window, as shown in the following figure.
Figure 10: Pop-up API Window
RadialGradient
setting to the circle. This gives the circle depth and makes it look more like a sphere. Main.fx
file, add the import statements that include the packages containing the RadialGradient
and Stop
classes used in the code that you are adding. Begin by typing the following line of code. import javafx.scene.paint.
Figure 11: Code Completion for RadialGradient
Class
Stop
class, which is also needed to define the radial gradient of the sphere. The following are the import statements that should have been added. import javafx.scene.paint.RadialGradient; import javafx.scene.paint.Stop;
fill
variable in the Main.fx
source file with the following source code in bold. You can copy the lines in bold, including the closing square brackets and braces for the fill
variable, and paste them into the source editor.
Stage { title: "First JavaFX App" scene: Scene { width: 250 height: 250 content: [ Circle { centerX: 100, centerY: 100, radius: 90, fill: RadialGradient { centerX: 75, centerY: 75, radius: 90 proportional: false stops: [ Stop { offset: 0.0 color: Color.web("#3B8DED") }, Stop { offset: 1.0 color: Color.web("#044EA4") } ] // stops } // RadialGradient } // Circle Text { font: Font { size: 22 } x: 20, y: 90 textAlignment: TextAlignment.CENTER content:"Welcome to /nJavaFX World" } //Text ] // content } // Scene } // Stage
Figure 12: Setting the Run Profile for the FirstJavaFXApp
Project
Figure 13: First Run of the JavaFX Sphere
In this section, you add the opacity and the animation that changes the sphere's opacity.
opacity
variable at the top of the source file and set its initial value to 1.0, as shown here in bold. import javafx.scene.paint.Color; import javafx.scene.paint.RadialGradient; import javafx.scene.paint.Stop; var opacity = 1.0;
Circle
object and bind this instance variable to the opacity
global variable, as shown in bold in the following code example. Circle { centerX: 100, centerY: 100, radius: 90, opacity: bind opacity fill: RadialGradient { centerX: 75 centerY: 75 radius: 90 proportional: false stops: [ Stop { offset: 0.0 color: Color.web("#3B8DED") }, Stop { offset: 1.0 color: Color.web("#044EA4") } ] // stops } // RadialGradient } //Circle
Stage
code block, as shown in the following figure.
Figure 14: Dragging Timeline Code Snippet From the Snippets View to the Source Editor
Animation occurs along a timeline, represented by ajavafx.animation.Timeline
object. Each timeline contains one or more key frames, represented by javafx.animation.KeyFrame
objects. For more information about animation, see Creating Animated Objects, a lesson in Building GUI Applications With JavaFX. time
instance variable from 1s
to 5s
and click Insert.Timeline
object literal appears as shown here.Timeline { repeatCount: Timeline.INDEFINITE, keyFrames : [ KeyFrame { time : 5s, canSkip: true } // KeyFrame ] // keyFrames } // Timeline
time
instance variable, five seconds, defines the elapsed time at which the values within the key frame will be set in a single cycle of the Timeline
object. The next couple of steps of this tutorial define those values that will be set.
canSkip
variable.
Figure 15: Dragging Values
Code Clip From the Snippets Window to the Source Editor
Values
defines the list of target variables and the desired values that they should interpolate at the specified time of the KeyFrame
.value
from 0.0
to 0.2
and the value of variable
to opacity
.values
variable is now as shown here in bold.Timeline { repeatCount: Timeline.INDEFINITE, KeyFrames : [ KeyFrame { time : 5s, canSkip: true values : [ opacity => 0.2 tween Interpolator.LINEAR ] // values } // KeyFrame ] // keyFrames } // Timeline
opacity
variable is defined within the five-second interval of the key frame. The =>
operator provides a literal constructor (a special notation) that makes it easier to express the list of key-interpolated values or object properties.tween
operator constructs the interpolated values for the opacity between 1.0 and 0.2 to create the gradual change in the sphere's opacity. LINEAR
to EASEBOTH
. Interpolator.EASEBOTH
is the built-in interpolator instance that provides for ease-in and ease-out behavior. KeyFrame
instance to provide a change of opacity from 0.2
to 1.0
within the next five-second interval. Copy the second KeyFrame
code block that is shown in bold in the following code example and paste it just below the first KeyFrame
object as shown next. Timeline { repeatCount: Timeline.INDEFINITE, keyFrames : [ KeyFrame { time : 5s, canSkip: true values : [ opacity => 0.2 tween Interpolator.EASEBOTH ] // values } // KeyFrame KeyFrame { time : 10s, canSkip: true values : [ opacity => 1.0 tween Interpolator.EASEBOTH ] // values } // KeyFrame ] // keyFrames } // Timeline
.play();
to the end of the Timeline
object declaration, as shown in bold in the following code example.play()
method plays the timeline as defined. The completed Timeline
object is shown here.
Timeline { repeatCount: Timeline.INDEFINITE, keyFrames: [ KeyFrame { time: 5s, canSkip: true values : [ opacity => 0.2 tween Interpolator.EASEBOTH ] // values } // KeyFrame KeyFrame { time: 10s, canSkip: true values : [ opacity => 1.0 tween Interpolator.EASEBOTH ] // values } // KeyFrame ] // keyFrames }.play();