lunes, 1 de septiembre de 2008

Funcionamiento de menú con JavaFX.

No seré yo el que ponga un pero al trabajo que realiza el personal en los códigos con JavaFX a los que se tienen acceso. Y más teniendo en cuenta que he sido testigo de la evolución y por tanto de las dificultades técnicas que ha sufrido esta tecnología. Pongo aquí cómo implemento un funcionamiento de menús sobre un par de arrays de nodos construidos como instancias de un nodo genérico de menú, algo más complejo que un CustomNode. (A ver cuándo puedo poner el visualizador de código tan útil en Blogger).

La clase nodo contiene un rectángulo con relleno de color aleatorio, un texto y además una variable timeline para el efecto de "aparecer".


/*
* una.fx
*
* Created on 22-sep-2008, 10:58:54
*/

package com.megestiono.widgets;

/**
* @author Alvaro
*/

import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.geometry.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.Font;
import javafx.scene.FontStyle;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;

public class Nodo extends CustomNode {

public attribute texto:String="N/A";
attribute opa:Number=0;
public attribute t = Timeline {
repeatCount: 1
keyFrames : [
KeyFrame {
time : 1s
values : [
opa => 1 tween Interpolator.LINEAR
]
}
]
}

public function aparece():Void{
t.start();
}

public function create(): Node {


var rnd = new java.util.Random();

return Group {
opacity: bind opa
content: [Rectangle {
x: 0, y: 0
width: 200, height: 200
fill: Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255))

},Text {
font: Font {
size: 20
style: FontStyle.PLAIN
}
x: 10, y: 40
content: texto
}]
};
}
}

Nodo {texto:"UnTexto"}

//La ventana desde donde los gestiono con un menú rudimentario construido con rectángulos.

import javafx.application.Frame;
import javafx.application.Stage;
import javafx.scene.layout.*;
import javafx.scene.geometry.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.*;
import javafx.application.WindowStyle;

var secuencia1:String[] = ["Uno","Dos","Tres"];
var secuencia2:String[] = ["Cuatro","Cinco","Seis"];
var arraynodos1:Nodo[] = [Nodo{texto:secuencia1[0]},Nodo{texto:secuencia1[1]},Nodo{texto:secuencia1[2]}];
var arraynodos2:Nodo[] = [Nodo{texto:secuencia2[0]},Nodo{texto:secuencia2[1]},Nodo{texto:secuencia2[2]}];
var nodos:Nodo[] = arraynodos1;

Frame {
title: "Menus"
width: 300
height: 300
closeAction: function() {
java.lang.System.exit( 0 );
}
visible: true
windowStyle: WindowStyle.TRANSPARENT

stage: Stage {
content: [VBox{
spacing:10
content:[HBox{
spacing:10
content:[Rectangle {
x: 0, y: 0
width: 50, height: 30
fill: Color.FORESTGREEN
onMousePressed: function (e){
for (nodo in nodos){
nodo.visible = false
}
nodos

[0].visible = true;
nodos
[0].t.start();
}
}, Rectangle {
x: 0, y: 0
width: 50, height: 30
fill: Color.FORESTGREEN
onMousePressed: function (e){
for (nodo in nodos){
nodo.visible = false
}
nodos

[1].visible = true;
nodos
[1].t.start();
}
}, Rectangle {
x: 0, y: 0
width: 50, height: 30
fill: Color.FORESTGREEN
onMousePressed: function (e){
for (nodo in nodos){
nodo.visible = false
}
nodos

[2].visible = true;
nodos
[2].t.start();
}
}, Rectangle {
x: 0, y: 0
width: 50, height: 30
fill: Color.RED
onMousePressed: function (e){

if (nodos == arraynodos2){
nodos = arraynodos1;
nodos

[0].visible = true;
nodos
[0].t.start();
} else {
nodos = arraynodos2;
nodos

[0].visible = true;
nodos
[0].t.start();
}
}
}]
}
,
Group{
content:bind nodos
},
Rectangle {
x: 270, y: 20
width: 50, height: 50
fill: Color.BLACK
onMouseClicked: function (e){
java.lang.System.exit( 0 );
}
}
]
}
]
}
}