Java Reference
In-Depth Information
Getting ready
This recipe uses classes from the
Image API
located in the
javafx.scene.image
package.
Using this API, you are able to configure, load, and control how your images are displayed using
the classes
Image
and
ImageView
. For this recipe, we will build a simple image browser to
illustrate the concepts presented here. The browser allows users to load an image by providing
its URL. You will use standard JavaFX controls, such as text boxes and buttons, to build the GUI.
If you are not familiar with the standard GUI controls, review the recipe
Creating a form with
JavaFX controls
from
Chapter 4
,
Components and Skinning
.
How to do it...
The code given next has been shortened to illustrate the essential portions involved in loading
and displaying an image. You can get a full listing of the code from
ch05/source-code/
src/image/ImageBrowserSimpleDemo.fx
.
def w = 800;
def h = 600;
var scene:Scene;
def maxW = w * 0.9;
def maxH = h * 0.9;
def
imgView
:ImageView =
ImageView
{
preserveRatio
:true
fitWidth
: maxW
fitHeight
:maxH
layoutX:(w-maxW)/2 layoutY:(h-maxH)/2
};
function
loadImg
(){
imgView.image
=
Image
{
url:(
scene.lookup
("addr") as TextBox).text
backgroundLoading:true
placeholder:Image{url:"{__DIR__}loading.jpg"}
}
}
def
addrBar
=
Group
{
layoutX: 20
layoutY: 20
content:HBox {
nodeVPos:VPos.CENTER
spacing:7
content:[
Label
{text:"Image URL:" textFill:Color.SILVER}
TextBox
{
id
:"addr" columns:80 promptText:"http://"
action:function(){loadImg()}
}




