Java Reference
In-Depth Information
How it works...
The previous code listing has been trimmed to its bare essentials to show you how to
download binary data of an image from a remote web server. There are two main places
in the code where you should focus your attention:
F
First, let's look at the declaration of the variable
gmapUrl
. This string composes
the URL, which is sent to the server to retrieve the image data for the map. The
string specifies all of the necessary URL parameters that the Google Map server
is expecting to render and return the image properly (see the Google Map API
documentation for details about expected parameters).
The important portion of the declaration of
gmapUrl
is the
on replace
trigger. Any
time that the expression for the URL value changes, the trigger automatically invokes
the
loadImage(url)
function, which is used to retrieve the binary data from the
server (see next bullet).
F
The
loadImage(url)
method is responsible for submitting the request to
retrieve the image from the remote web server using the
HttpResponse
object. Here again, we provide a callback event-handling function through the
onInput:function(:InputStream)
property. This function is invoked when
all of the bytes for the binary resource have been downloaded. In the code, we first
attempt to detect if the data is available using
InputStream.available() > 0
;
if so, we generate the image, as follows:
The code uses the Java class
javax.imageio.ImageIO
to
generate an instance of
java.awt.image.BufferedImage
from
the received
InputStream
object.
Since we can't use
BufferedImage
directly to display the image,
the code then uses the
javafx.ext.Swing.SwingUtils
to
convert the buffered image into an instance of
javafx.scene.
image.Imge
by calling
SwingUtils.toFXImage()
. The image
is then made visible by assigning it to the
ImageView
instance,
imgView
, through its
image
property.
The omitted portion of the code is composed of the following GUI control elements:
F
Slider
—the slider is used to control the zoom level of the image
F
TextBox
—the text box captures the address or location for the map
F
Map button
—forces the application to request an illustrated road map
F
Satellite button
—requests a map with satellite image overlays


