jueves, 25 de junio de 2009

Code: Ten minute introduction to JavaFX




package tutorial;

import java.lang.Thread;
import javafx.data.pull.PullParser;
import javafx.data.xml.QName;
import javafx.io.http.HttpRequest;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


def sceneWidth = 425;
def sceneHeight = 300;

var sunrise: String; // sunrise time
var sunset: String; // sunset time

var topRectangle: Rectangle = Rectangle {
translateX: 0
translateY: 10
width: sceneWidth - 2
height: 60
stroke: Color.DARKBLUE
strokeWidth: 1
arcWidth: 32
arcHeight: 32
fill: LinearGradient {
endY: 0
stops: [
Stop {
offset: 0
color: Color.DARKBLUE
}
Stop {
offset: 1
color: Color.LIGHTBLUE
}
]
}
}

var bottomRectangle: Rectangle = Rectangle {
translateX: 2
translateY: 160
width: 400
height: 60
stroke: Color.DARKBLUE
arcWidth: 32
arcHeight: 32
fill: LinearGradient {
endY: 0
stops: [
Stop {
offset: 0
color: Color.DARKBLUE
}
Stop {
offset: 1
color: Color.LIGHTBLUE
}
]
}
}

var result: String = " Sunrise: {sunrise} \n Sunset: {sunset}" on replace oldValue {
println("replace");
}

var textResult = Text {
wrappingWidth: 200
translateX: 25
translateY: 100
content: bind result ;
fill: Color.DARKBLUE
font: Font { size: 18
}
}



var rightRectangle: Rectangle = Rectangle {
translateX: topRectangle.width - 210
translateY: 10
width: 210
height: 60
stroke: Color.DARKBLUE
arcWidth: 32
arcHeight: 32
fill: LinearGradient {
endY: 0
stops: [
Stop {
offset: 0
color: Color.DARKBLUE
}
Stop {
offset: 1
color: Color.LIGHTBLUE
}
]
}
}

var text = Text {
x: 10
y: 50
font: Font { size: 20
}
fill: Color.WHITE
content: " Barajas - Madrid "
}

var url = "http://weather.yahooapis.com/forecastrss?p=SPXX0050&u=c";
println(" url.length : {url.length()} ");
if (url.length() == 55 ) {
result = " Getting data .... ";

HttpRequest {

location: url
onDone: function() {
print ("done");
// Simulating a slow service
Thread.sleep(1000);
result = " Sunrise: {sunrise} \n Sunset: {sunset}" ;
}
onInput: function(input) {
try {
PullParser {
input: input
onEvent: function(event) {
if ((event.type == PullParser.START_ELEMENT) and (event.qname.prefix == "yweather")) {
if (event.qname.name == "astronomy") {
sunrise = event.getAttributeValue(QName{name: "sunrise"
});
println("sunrise = {sunrise}");


sunset = event.getAttributeValue(QName{name: "sunset"
});
println(" sunset = {sunset}");

}
}

}

}.parse()

} finally {
input.close();
}

}
}
}.start();

// Image
var sunImage: Image = Image {
url: "{__DIR__}media/sun.png"
}

var sunView: ImageView = ImageView {
translateX: 200
translateY: 50
image: sunImage
}

Stage {
title: " Sunrise and sunset in Barajas "
width: sceneWidth
height: sceneHeight
style: StageStyle.TRANSPARENT
scene: Scene {
width: sceneWidth
height: sceneHeight
content: Group {
content: bind [ topRectangle, text, rightRectangle, textResult, bottomRectangle, sunView ]

clip: Rectangle {
width: sceneWidth
height: sceneHeight
arcWidth: 32
arcHeight: 32
}
}
fill: Color.TRANSPARENT
}
}




Related post: Ten minute introduction to JavaFX

No hay comentarios:

Publicar un comentario