Monday, July 14, 2008

sample code on JavaFX i.e., Fullscreen window, center text in nodes, Dynamic menu with alert window, using functions as parameters, FXShell class prov

//*** Sample Code: Dynamic menu with alert window
//Dynamic menu. Alert window with item number is displayed when a menu item is clicked.

import javafx.ui.*;
import java.lang.System;

class AlertModel {
attribute AlertText:String;
attribute ShowAlert:boolean;
}
var am = AlertModel {
AlertText: "Text"
ShowAlert: false
};
class MenuSource {
attribute items:MenuItem*;
attribute malert:AlertModel;
operation addNewItem(mtext:String,mcommand:String);
operation menuCommand(item:String);
}
trigger on new MenuSource {
for (i in [0..3]) {
addNewItem("Item {i}","{i}");
}
}
operation MenuSource.menuCommand(item:String) {
System.out.println(item);
malert.AlertText="Item {item} clicked";
malert.ShowAlert=true;
}
operation MenuSource.addNewItem(mtext:String , mcommand:String ) {
var mi = MenuItem {
text: mtext
action: operation() {
menuCommand(mcommand);
}
};
insert mi as last into items;
}
var ms = new MenuSource;
ms.malert=am;
var menu = MenuBar {
menus: Menu {
text: "Test"
items: bind ms.items

}
};
var del_button = Button {
text: "Press to delete"
action: operation() {
var n = sizeof ms.items - 1;
delete ms.items[indexof . == n];
}
};
var add_button = Button {
text: "Press to add"
action: operation() {
var n = sizeof ms.items;
ms.addNewItem("Item {n}" , "{n}");
}
};
var alert = Frame {
visible: bind am.ShowAlert
content: BorderPanel {
top: Label { text: bind am.AlertText }
bottom: Button {
text: "Close"
action: operation() {
am.ShowAlert=false;
}
}
}
};
Frame {
menubar: menu
content: BorderPanel { top: del_button
bottom: add_button }
visible: true
onClose: operation() {System.exit(0);}
}

//how to center text in nodes.

import javafx.ui.*;
import javafx.ui.canvas.*;

var contentText = "Sardegna"; //very nice island, go there once ;-)
var myFont = Font {face: VERDANA, style: [BOLD], size: 50};

Frame {
content:
Canvas {
var: canvas
height: 70
width: 450
border: null
content:
Group {
var w = bind canvas.width
var h = bind canvas.height
content:
[Rect {
width: bind w
height: bind h
fill: LinearGradient {
x1: 0
y1: 0
x2: 1
y2: 0
stops:
[Stop {offset: 0, color: green},
Stop {offset: .5, color: new Color(.5, 1, 0, 1)},
Stop {offset: 1, color: green}]
}
stroke: green
strokeWidth: 1
},
Text {
transform: bind translate(w/2, h/2)
//verticalAlignment: BASELINE
valign: MIDDLE
halign: CENTER
content: contentText
font: myFont
fill: darkgreen
}]
}
}
}

// This is a simple example showing "Hello World" centered on a fullscreen frame. This sample is broken due to language changes.
import javafx.ui.*;
import javafx.ui.canvas.*;
import javafx.ui.filter.*;
import java.awt.Toolkit;

Frame {
var s = Toolkit.getDefaultToolkit().getScreenSize()
title: "Hello"
undecorated: true
background: white
width: s.width
height: s.height
content: Canvas {
content:
Text {
var: hello
x: bind (s.width / 2) - (hello.currentWidth / 2)
y: bind (s.height / 2) - (hello.currentHeight / 2)
content: "Hello World!"
font: Font {face: VERDANA, style: [ITALIC, BOLD], size: 60
}
}
}
visible: true
}
//This is a simple example about using functions as parameters.
import java.lang.System;
function reduce(f: function(a:*,b:*), list: **, nilArg: *) {
return if( list == [] ) then
nilArg
else
f( list[indexof . == 0],
reduce(f, list[indexof .> 0], nilArg));
}
function sum( x: Number, y: Number ): Number {
return x+y;
}
function product( x: Number, y: Number ): Number {
return x*y;
}
var list = [1,2,3,4];
operation p( s: String ) { System.out.println( s ); }
p("# sum : {reduce(sum,list,0)}");
p("# product : {reduce(product,list,1)}");
//***********************//

//The simplest, though unsupported, way is by calling the main() method in the FXShell class provided by the JavaFX jars:
import net.java.javafx.FXShell;

public class FxMainLauncher {
public static void main(String[] args) throws Exception {
FXShell.main(new String[] {"HelloWorld.fx"});
}
}

0 comments: