Java Reference
In-Depth Information
This chapter shows you how to use the supported media rendering components, including
ImageView, MediaPlayer, and MediaView. These components provide high-level APIs that let
developers create applications with engaging and interactive media content.
Accessing media assets
In previous chapters, you have seen the use of variable
__DIR__
when accessing local
resources, but little detail was offered about its purpose and how it works. So, what does that
special variable store? In this recipe, we will explore how to use the
__DIR__
special variable
and other means of loading resources locally or remotely.
Getting ready
The concepts presented in this recipe are used widely throughout the JavaFX application
framework when pointing to resources. In general, classes that point to a local or remote
resource uses a string representation of a URL where the resource is stored. This is especially
true for the
ImageView
and
MediaPlayer
classes discussed in this chapter.
How to do it...
This recipe shows you three ways of creating a URL to point to a local or remote resource used
by a JavaFX application. The full listing of the code presented here can be found in
ch05/
source-code/src/UrlAccess.fx
.
Using the
__DIR__
pseudo-variable to access assets as packaged resources:
var resImage = "{__DIR__}image.png";
Using a direct reference to a local file:
var localImage =
"file:/users/home/vladimir/javafx/ch005/source-code/src/image.png";
Using a URL to access a remote file:
var remoteImage = "http://www.flickr.com/3201/2905493571_a6db13ce1b_d.
jpg"
How it works...
Loading media assets in JavaFX requires the use of a well-formatted URL that points to the
location of the resources. For instance, both the
Image
and the
Media
classes (covered
later in this chapter) require a URL string to locate and load the resource to be rendered.
The URL must be an absolute path that specifies the fully-realized scheme, device, and
resource location.






