Pro JavaFX Platform Chap3. Ex1.

2009년 8월 6일 목요일

The Stage Coach

image

package projavafx.stagecoach.ui;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javafx.util.Properties;

import javafx.io.Storage;
import javafx.io.Resource;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Alert;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

var args = FX.getArguments();
var stageStyle = StageStyle.DECORATED;
if (sizeof args >= 1) {
if (args[0].toLowerCase() == "transparent") {
stageStyle = StageStyle.TRANSPARENT;
}
else if (args[0].toLowerCase() == "undecorated"){
stageStyle = StageStyle.UNDECORATED;
}
}
var resizable:Boolean = true;
var fullScreen:Boolean = false;
var title:String = "Stage Coach";
var stageRef:Stage;
var entry:Storage;

function saveProperties():Void {
println("Storage.list():{Storage.list()}");
entry = Storage {
source: "stagecoach.properties"
};
var resource:Resource = entry.resource;
var properties:Properties = new Properties();
properties.put("xPos", "{stageRef.x}");
properties.put("yPos", "{stageRef.y}");
try {
var outputStream:OutputStream = resource.openOutputStream(true);
properties.store(outputStream);
outputStream.close();
println("properties written");
}
catch (ioe:IOException) {
println("IOException in saveProperties:{ioe}");
}
}

function loadProperties():Void {
println("Storage.list():{Storage.list()}");
entry = Storage {
source: "stagecoach.properties"
};
var resource:Resource = entry.resource;
var properties:Properties = new Properties();
try {
var inputStream:InputStream = resource.openInputStream();
properties.load(inputStream);
inputStream.close();
if (properties.get("xPos") != null) {
stageRef.x = Double.parseDouble(properties.get("xPos"));
stageRef.y = Double.parseDouble(properties.get("yPos"));
}
println("properties read");
}
catch (ioe:IOException) {
println("IOException in loadProperties:{ioe}");
}
stageRef.visible = true;
}

function showSystemProperties():Void {
Alert.inform("Some System Properties",
"java.os.name: {FX.getProperty("javafx.os.name")}\n"
"javafx.os.arch: {FX.getProperty("javafx.os.arch")}\n"
"java.os.version: {FX.getProperty("javafx.os.version")}");
}

stageRef = Stage {
visible: false
title: bind title
width: 330
height: 550
style: stageStyle
resizable: bind resizable
fullScreen: bind fullScreen
onClose: function():Void {
println("Stage is closing");
}
scene:Scene {
fill: Color.TRANSPARENT
content: [
Rectangle {
width: 300
height: 500
arcWidth: 50
arcHeight: 50
fill: Color.SKYBLUE
onMouseDragged: function(me:MouseEvent):Void {
stageRef.x += me.dragX;
stageRef.y += me.dragY;
}
onMousePressed: function(me:MouseEvent):Void {
if (me.popupTrigger) showSystemProperties();
}
onMouseReleased: function(me:MouseEvent):Void {
if (me.popupTrigger) showSystemProperties();
}
},
VBox {
layoutX: 20
layoutY: 20
spacing: 10
content: [
Text {
textOrigin: TextOrigin.TOP
font: Font.font("Sans Serif", 14)
content: bind "x: {stageRef.x}"
},
Text {
textOrigin: TextOrigin.TOP
font: Font.font("Sans Serif", 14)
content: bind "y: {stageRef.y}"
},
Text {
textOrigin: TextOrigin.TOP
font: Font.font("Sans Serif", 14)
content: bind "height: {stageRef.height}"
},
Text {
textOrigin: TextOrigin.TOP
font: Font.font("Sans Serif", 14)
content: bind "width: {stageRef.width}"
},
Text {
textOrigin: TextOrigin.TOP
font: Font.font("Sans Serif", 14)
content: bind "containsFocus: {stageRef.containsFocus}"
},
CheckBox {
blocksMouse: true
text: "resizable"
selected: bind resizable with inverse
},
Text {
textOrigin: TextOrigin.TOP
font: Font.font("Sans Serif", 14)
content: bind "resizable: {stageRef.resizable}"
},
CheckBox {
blocksMouse: true
text: "fullScreen"
selected: bind fullScreen with inverse
},
Text {
textOrigin: TextOrigin.TOP
font: Font.font("Sans Serif", 14)
content: bind "fullScreen: {stageRef.fullScreen}"
},
Label {
font: Font.font("Sans Serif", 14)
text: "title:"
},
TextBox {
blocksMouse: true
text: bind title with inverse
columns: 15
},
Button {
text: "toBack()"
action: function():Void {
stageRef.toBack();
}
},
Button {
text: "toFront()"
action: function():Void {
stageRef.toFront();
}
},
Button {
text: "close()"
action: function():Void {
saveProperties();
stageRef.close();
}
},
Button {
text: "FX.exit()"
action: function():Void {
if (Alert.question("Are You Sure?",
"Exit without saving screen position?")) {
FX.exit();
}
}
}
]
}
]
}
}
stageRef.x = (Screen.primary.visualBounds.width - stageRef.width) / 2;
stageRef.y = (Screen.primary.visualBounds.height - stageRef.height) / 4;
loadProperties();

0 개의 댓글: