JavaFX
We will be using JavaFX for the ChessForge GUI assignment. JavaFX is a toolkit for building graphical user interfaces that contains a multitude of widgets and features. You can learn a lot about JavaFX from the Oracle tutorial. This guide contains a collection of simple examples which should guide you in the development of your GUI.
Remember: if you have any questions about how to use particular JavaFX classes, check the JavaFX API.
Getting Started
To start writing your JavaFX application, complete the following steps:
- Create a subclass of
Application
. - In your
main
, callApplication.launch
with your application class as the only argument. - In the
start
method of your application class, set aScene
on the providedStage
. TheScene
’s only constructor argument should be the JavaFX component (such as aButton
) that you want to display. - Call
sizeToScene
andshow
on theStage
.
Code | Appearance |
---|---|
|
|
Listeners / Event Handlers
We react to events such as button presses or mouse clicks not by modifying the JavaFX component but by providing it a listener to call. Such listeners are also called handlers or callbacks. In the following program, the button prints a message to the terminal when pressed.
Code | Appearance |
---|---|
|
|
Parent Containers
JavaFX components such as buttons or text labels may be grouped together in order to control how they appear together in the window. Components are grouped by placing them in the same Parent
. The JavaFX libraries include several types of Parent
; the following are a few examples you may be interested in.
Code | Appearance |
---|---|
|
|
|
|
|
|
|
|
Images and Resources
JavaFX includes an ImageView
class which can be used to display an image. The Java standard libraries include code to load most common types of image files. Key to this is knowing how to get the files you want to use.
It’s possible to access the contents of the filesystem using the File
class, but this makes your Java program depend upon the filesystem layout of the user’s computer. For a more portable solution, we can refer to the contents of the classpath: the set of directories where Java normally looks for .class
files. This way, it’s possible to package your .class
files into a JAR along with your other resources; your application is contained within a single archive file instead of in many files throughout the disk.
To do this in IntelliJ, you merely need to mark the directory containing the resources you want as a “resources root”. Once you have done so, you can use the path of the file relative to that root. For instance, suppose a directory in your project is named images
. To load a file images/sprites/frog.png
, we would use the string /sprites/frog.png
.
Code | Appearance |
---|---|
|
|
Effects
JavaFX components may be decorated with Effect
objects to transform their appearance. As with Parent
s, a number of Effect
s exist in the API; check the documentation for the abstract class Effect
to learn more. Here are examples of a few.
Code | Appearance |
---|---|
|
|
|
|
|
|
Chaining Effects
Effects can be chained by the setInput
method. This method allows an Effect
to execute the provided Effect
before it is applied itself. That is, b.setInput(a)
will cause the b
to apply a
first and then itself.
Code | Appearance |
---|---|
|
|