JavaFX Uni Parthenope

Preview:

DESCRIPTION

JavaFX: la piattaforma Java per le Rich Internet Application

Citation preview

Corrado De BariSoftware & Java Ambassador

Sun Microsystems Italia Spa

JavaFX: la piattaforma Java per le Rich Internet Applications

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 2

Agenda

● What's news in Java Runtime Environment● JavaFX Technology– Key Features– Architecture & Tools– JavaFX Script tutorial

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

New Features in 6u10● JavaKernel: faster download and install● Java Quick Starter: faster applet start● Rewritten Java Plugin

– Applets run in own JVM– Unified desktop / browser deployment– Draggable Applet– Scriptable applets – Direct3D© support

Try: http://download.java.net/javadesktop/plugin2/wwj/

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 4

Architectural Overview

HTML Web Page

Applet 1

Applet 2

Applet 3

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Architectural Overview

● Ground-up rewrite of the support for applets in the web browser● Applets are no longer executed in a Java Virtual Machine (JVM™) inside the

web browser● Instead, a separate JVM machine process is launched to execute the applets

● By default, only one JVM machine is launched

– Same resource consumption and sharing properties as the “classic” Java technology-based Plug-In

● Opportunity to launch more than one JVM machine

– To support per-applet command-line arguments, heap size requests, and more

● Architectural similarities to Java Web Start software, but tighter browser integration

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 6

Architectural Overview

Thin Server JVM

Client JVM 1

Client JVM 2OS Process 1

OS Process 2

OS Process 3

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 7

Compatibility● Backward compatibility based on runs of hundreds of

applets from throughout the web● Applet lifecycle unchanged

– init(), start(), stop(), destroy()● All services supported:

Browser proxy settings Browser certificate storeBrowser authenticator Browser cookie storePrintingJava/JavaScript integration

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 8

JNLP SupportMost significant new feature● Unifies deployment between Java Web Start and the

Java Plug-In– Simply choose the top-level container (Frame /

Applet)● Incorporate JNLP extensions trivially in applets

– JavaFX run-time libraries, JOGL, Java 3D, JAI● Use JNLP APIs from applets

– PersistenceService, DownloadService, ...● Full support for JVM command-line arguments, JRE

version selection, JRE auto-download

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Basic JNLP Example

<applet width=”500” height=”500”> <param name=”jnlp_href” value=”my_applet.jnlp”></applet>

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Basic JNLP Example<?xml version=”1.0” encoding=”UTF-8”?><jnlp href=”my_applet.jnlp”> <information> <title=”My Applet”> <vendor>My Company, Inc.</vendor> <offline-allowed /> </information> <resources> <j2se version=”1.4+” href=”http://java.sun.com/products/autodl/j2se” /> <jar href=”my_jar_1.jar” main=”true” /> <jar href=”my_jar_2.jar” /> </resources> <applet-desc name=”My Applet” main-class=”com.mycompany.MyApplet” width=”1” height=”1” /></jnlp>

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Applet-Desc Tag<?xml version=”1.0” encoding=”UTF-8”?><jnlp href=”my_applet.jnlp”> <information> <title=”My Applet”> <vendor>My Company, Inc.</vendor> <offline-allowed /> </information> <resources> <j2se version=”1.4+” href=”http://java.sun.com/products/autodl/j2se” /> <jar href=”my_jar_1.jar” main=”true” /> <jar href=”my_jar_2.jar” /> </resources> <applet-desc name=”My Applet” main-class=”com.mycompany.MyApplet” width=”1” height=”1” /></jnlp>

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Applet-Desc Tag

● width and height are ignored– Browser always overrides because it knows best how

big the applet needs to be– Relative width and height supported in applet tag (i.e.,

“50%”)● main-class parameter always used

– code parameter from applet tag is ignored– Enables backward compatibility mechanisms

(described later)

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 13

Java / JavaScript Support

● Completely rewritten Java / JavaScript integration● More complete, reliable and portable than before● Formerly Mozilla-specific functionality now working in

Internet Explorer– Static method access– Construction of new Java objects from JavaScript

● Respecifying and reintroducing “LiveConnect”

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 14

DOM Access

Enables AJAX-style web apps using Java language● DOM access via W3C APIs has been rewritten

– Built on top of new Java/JavaScript support● New, much simpler bootstrapping entry point

org.w3c.dom.Document doc = com.sun.java.browser.plugin2.DOM. getDocument(applet);

● Can then cast to HTMLDocument and descend into the DOM● Perform operations from any thread

– Underlying Java/JavaScript bridge is thread safe

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 15

Support for Advanced Applets● Control over Java heap size● Control over Java 2D acceleration parameters● Ideal environment for running 3D content● JOGL applet examples– NASA World Wind Java:

● http://download.java.net/javadesktop/plugin2/wwj/

– Jake2 – Bytonic Software:● http://www.bytonic.de/downloads/jake2_jogl11.jnlp

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

JavaFX

JavaFX is THE platform for creating and delivering

Rich Internet Applications across all the screens of your life

JavaFX is Powered by Java

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Key Features

✔ One-stop shop RIA platform for all screens: ✔ Build engaging visual experiences across desktop,

browser and mobile with a unified development and deployment model.

✔ Broadest market reach: ✔ Distribute RIAs easily across billions of devices with the

power of Java.✔ Designer-developer workflow: ✔ Dramatically shorten your production cycle for design

and development.

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Key Features (Continued)

✔ Powerful runtime: ✔ Leverage the extreme ubiquity, power, performance

and security of the Java runtime.✔ Break free from the browser: ✔ Drag-and drop a JavaFX application from the browser

to deploy to the desktop.✔ Java technology compatibility: ✔ Preserve your investment by enabling the use of any

Java library within a JavaFX application.

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

What you can do: Simple Video Player

•Incorporating video in your application is as simple as creating an instance of this component, setting a few variables and including a link to your video source.

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

3-D Display Shelf: the Perspective Transform

•The PerspectiveTransform built into JavaFX can be used to easily create 3-D effects

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Flying Saucer

•The new out-of-process Java applet plugin in Java SE 6 update 10 enables you to make applets which can run outside of the browser.

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Demo: JavaFX apps

http://www.javafx.com/samples/EffectsPlayground/webstart/EffectsPlayground.jnlphttp://www.javafx.com/samples/DraggableMP3Player/webstart/DraggableMP3Player.jnlphttp://www.javafx.com/samples/BrickBreaker/webstart/BrickBreaker.jnlp

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

JavaFX architecture

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 24

JavaFX roadmap

JavaFX Desktop

(Winter 2008)JavaFX Mobile

(Spring 2009)

JavaFX TV(Summer 2009)

Desktop Product Line

Mobile Product Line

TV Product Line

Other Platforms With Partner platforms/OSs

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 25

JavaFX Tools: rel 1.2

NetBeans IDE 6.5.1 for JavaFX 1.2 ✔ Windows XP, Windows Vista ✔ Mac OS X 10.4.10 minimum✔ Ubuntu 8.04 LTE✔ OpenSolaris 2009.06

JavaFX 1.2 Production Suite✔ Adobe Illustrator CS3/CS4✔ Adobe Photoshop CS3/CS4

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 26

JavaFX ToolsJavaFX 1.2 Plugin for Adobe Photoshop and JavaFX 1.0 Plugin for Adobe Illustrator

JavaFX 1.2 Media Factory✔SVG Importer/Transcoder✔Viewer

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 27

How to Deploy

● Java Plugin● Java Web Start● JavaFX 1.0 Mobile Emulator

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

JavaFX APIs● JavaFX Scene

– Node based GUI framework● JavaFX Media

– Media player, native and cross platform codecs● JavaFX Web Services

– REST-ful – RSS

● JavaFX Profiles– Common– Desktop– Mobile

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

JavaFX Script

● Created for rapid and easy GUI creation● Object-oriented

– Namespaces, Classes, attributes, functions and operations

● Declarative syntax● Both static-typing & type-inference● Animation, data binding and event handing

built in

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 30

Example: Hello World in Swing

import javax.swing.*;

public class HelloWorldSwing {

public static void main(String[] args) { JFrame frame = new JFrame("HelloWorld Swing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation(

JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 31

Example: Hello World in JavaFXDeclarative SyntaxFrame { title: "Hello World JavaFX" width: 200 height: 100 visible: true stage: Stage { content: [Text { font: Font { size: 24 style: FontStyle.PLAIN } x: 10, y: 30 content: "Hello World" }] }}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Class Definition

•Address class definition: The Address class declares street, city, state, and zip instance variables all of type String

class Address { var street: String; var city: String; var state: String; var zip: String;}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Class Definition•Customer class definition with functions - functions are like methods in Java

class Customer { var firstName: String; var lastName: String; var phoneNum: String; var address: Address;

function printName() { println("Name: {firstName} {lastName}"); }

function printPhoneNum(){ println("Phone: {phoneNum}"); }}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Create an Object •Nesting Address object inside Customer objectdef customer = Customer {

firstName: "John"; lastName: "Doe"; phoneNum: "(408) 555-1212"; address: Address { street: "1 Main Street"; city: "Santa Clara"; state: "CA"; zip: "95050"; }}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Step by Step Process of Creating a simple GUI

•JavaFX application that renders a green rounded rectangle and a white circle with red outline on the top of the rectangle

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Step by Step Process of Creating a GUI

1.Add necessary imports2.Create an Application window3.Set the Scene4.Create a Rectangle5.Create a Circle6.Change the code (so that the Circle gets underneath the Rectangle)

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

1. Add Necessary Importsimport javafx.stage.Stage; // required to render a window

import javafx.scene.Scene; // required to display a circle and

// rectangle on a window

import javafx.scene.shape.Rectangle; // required to render the rectangle

import javafx.scene.paint.Color; // required to fill and stroke the

// rectangle and circle with color

import javafx.scene.shape.Circle; // required to render the circle

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

2. Create an Application Window•In order to display the graphics, you need to create a window through Stage object literal// Create a Stage object literal. Stage is required to render any object.// The window should be big enough to display all of the rectangle and // the circle. In this example, the window is a rectangle of 249 by 251// pixels. To create a window of this size, with a "Declaring is Easy" title,// declare these values within the curly brackets using the following

code:

Stage { title: "Declaring Is Easy!" width: 249 height: 251 visible: true}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

3. Set the Scene •Within the stage, set the scene to hold Node objects such as a circle or a rectangle•The scene is a root area where you place objects of the node type. •The scene has content variable that is used to hold the nodes. •There are many different kinds of nodes, such as graphical objects, text, and GUI components.

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

3. Set the Scene (Continued)•// The content of the window becomes filled with white because // white is a default fill color for a scene. The scene is

placed on top // of the window.

Stage {

... scene: Scene { content: [ ] } }

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

4. Create a RectangleStage { ... scene: Scene { content: [ // The x and y instance variables specify the pixel location of the // rectangle, arcWidth and arcHeight define the roundness of // corners, and the fill variable defines the color that fills the rectangle. Rectangle { x: 45 y: 35 width: 150 height: 150 arcWidth: 15 arcHeight: 15 fill: Color.GREEN } ] } }

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

5. Create a CircleStage { ... scene: Scene { content: [ Rectangle { ... }, // Because the rectangle is declared before any other objects, it is painted // first. The rectangle will be behind any other objects painted later. Circle{ centerX: 118 centerY: 110 radius: 83 fill: Color.WHITE stroke: Color.RED } ] } }

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

6. Place Circle Underneath RectangleStage { ... scene: Scene { content: [ // Place the circle underneath the square. To do so, switch the order // of the circle and square Circle{ .... }, Rectangle { ... } ] } }

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

Effects: DropShadow

Text { effect: DropShadow { offsetY: 3 color: Color.color(0.4, 0.4, 0.4) }; ... }, Circle { effect: DropShadow { offsetY: 4 }, ... }

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 45

ImagesImageView = ImageView { clip: Rectangle { y: 30 x: 50 width: 350 height: 100 } image: Image { url: "..."}}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

JavaFX Script: Data Binding

// will be modified elsewhere

var count:Integer = new Integer();

// The label text is bound to the count

Label {

text: bind count.value.toString( )

}

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009

JavaFX Script: Event Triggers

// Triggering validation of the

// number of wheels on a vehicle

attribute numWheels:Integer = 4

on replace (oldValue) {

if (numWheels <= 0) {

System.out.println("{numWheels} wheels not valid, changing back to {oldValue}!");

numWheels = oldValue;

}

};

Sun Microsystems ProprietaryTechDay 09 – UniParthenope, 4 giugno 2009 48

Resources

● JavaPassion Trainings (Source of this presentation):– http://www.javapassion.com/javafx

● http://www.javafx.com/● http://java.sun.com/javase/downloads/index.jsp● http://java.sun.com/javase/6/webnotes/6u10.html● http://scenegraph.dev.java.net/● http://jogl.dev.java.net/

Corrado De BariSoftware & Java Ambassador

Sun Microsystems Italia Spa

JavaFX: la piattaforma Java per le Rich Internet Applications