2009-03-18
Reflection Example for HBox and VBox
「HBox と VBox をリフレクションで作れるか」という コメント を頂いたので、サンプルを作ってみました。
This is my answer to the question. Please refer to the following code.
実行結果 (result)
package fooami.reflection;
import javafx.reflect.*;
/**
* Reflection Example.
* @author hide1080
*/
var color1 = javafx.scene.paint.Color.RED;
var color2 = javafx.scene.paint.Color.BLUE;
var color3 = javafx.scene.paint.Color.YELLOW;
var color4 = javafx.scene.paint.Color.GREEN;
function run(args: String[]) {
var context = FXLocal.getContext();
// リフレクションで4個の Rectangle を作成する。
// Create four mirror objects by reflection api for rectangle.
var classRect = context.findClass("javafx.scene.shape.Rectangle");
var objValRect1 = classRect.allocate();
objValRect1.initVar("translateX", context.mirrorOf(5.0));
objValRect1.initVar("translateY", context.mirrorOf(5.0));
objValRect1.initVar("width", context.mirrorOf(100.0));
objValRect1.initVar("height", context.mirrorOf(100.0));
objValRect1.initVar("fill", context.mirrorOf(color1));
objValRect1 = objValRect1.initialize();
var objValRect2 = classRect.allocate();
objValRect2.initVar("translateX", context.mirrorOf(5.0));
objValRect2.initVar("translateY", context.mirrorOf(5.0));
objValRect2.initVar("width", context.mirrorOf(100.0));
objValRect2.initVar("height", context.mirrorOf(100.0));
objValRect2.initVar("fill", context.mirrorOf(color2));
objValRect2 = objValRect2.initialize();
var objValRect3 = classRect.allocate();
objValRect3.initVar("translateX", context.mirrorOf(5.0));
objValRect3.initVar("translateY", context.mirrorOf(5.0));
objValRect3.initVar("width", context.mirrorOf(100.0));
objValRect3.initVar("height", context.mirrorOf(100.0));
objValRect3.initVar("fill", context.mirrorOf(color3));
objValRect3 = objValRect3.initialize();
var objValRect4 = classRect.allocate();
objValRect4.initVar("translateX", context.mirrorOf(5.0));
objValRect4.initVar("translateY", context.mirrorOf(5.0));
objValRect4.initVar("width", context.mirrorOf(100.0));
objValRect4.initVar("height", context.mirrorOf(100.0));
objValRect4.initVar("fill", context.mirrorOf(color4));
objValRect4 = objValRect4.initialize();
// リフレクションで2個の HBox を作成する。
// Create two HBox mirror objects, then,
// they have a couple of rectangle's mirror in each.
var classHBox = context.findClass("javafx.scene.layout.HBox");
var objValHBox1 = classHBox.allocate();
objValHBox1.initVar("spacing", context.mirrorOf(5.0));
objValHBox1 = objValHBox1.initialize();
// initVar でシーケンスタイプのフィールドを初期化しようとすると
// 内部で FXLocal.SequenceValue から FXLocal.ObjectValue への
// ClassCastException となってしまうので FXVarMember を介して
// シーケンスを設定する。
// Note: because ClassCastException (from FXLocal.SequenceValue
// to FXLocal.ObjectValue) happened internally when it tried to
// initialize the field of the sequence type with initVar(),
// I'll set via FXVarMember.
var seqValue = context.makeSequence(objValRect1.getType(),
objValRect1, objValRect2);
var varMember = classHBox.getVariable("content");
varMember.setValue(objValHBox1, seqValue);
var objValHBox2 = classHBox.allocate();
objValHBox2.initVar("spacing", context.mirrorOf(5.0));
objValHBox2 = objValHBox2.initialize();
seqValue = context.makeSequence(objValRect1.getType(),
objValRect3, objValRect4);
varMember.setValue(objValHBox2, seqValue);
// VBox を作成する。
// Create a VBox.
var classVBox = context.findClass("javafx.scene.layout.VBox");
var objValVBox = classVBox.allocate();
objValVBox.initVar("spacing", context.mirrorOf(5.0));
objValVBox = objValVBox.initialize();
seqValue = context.makeSequence(objValHBox1.getType(),
objValHBox1, objValHBox2);
varMember = classVBox.getVariable("content");
varMember.setValue(objValVBox, seqValue);
// Scene を作成する。
// Create a Scene.
var classScene = context.findClass("javafx.scene.Scene");
var objValScene = classScene.allocate();
objValScene.initVar("width", context.mirrorOf(215.0));
objValScene.initVar("height", context.mirrorOf(215.0));
objValScene = objValScene.initialize();
seqValue = context.makeSequence(objValVBox.getType(), objValVBox);
varMember = classScene.getVariable("content");
varMember.setValue(objValScene, seqValue);
// Stage を作成する。
// Create a Stage.
var classStage = context.findClass("javafx.stage.Stage");
var objValStage = classStage.allocate();
objValStage.initVar("title", context.mirrorOf("Reflection Example"));
objValStage.initVar("scene", objValScene);
objValStage = objValStage.initialize();
var stage = (objValStage as FXLocal.Value).asObject();
return stage;
}
トラックバック - http://d.hatena.ne.jp/hide1080/20090318/1237386063
リンク元
- 7 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4DAJP_jaJP256JP257&q=NetBeansでJavaFX
- 3 http://d.hatena.ne.jp/keyword/Silverlight
- 3 http://reader.livedoor.com/reader/
- 3 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&hs=WtC&q=IE7互換+ie8&btnG=検索&lr=lang_ja
- 2 http://d.hatena.ne.jp/keyword/JavaFX
- 2 http://www.google.co.jp/search?hl=ja&q=夢の車&btnG=Google+検索&meta=lr=&aq=f
- 2 http://www.google.co.jp/search?hl=ja&q=pdf繧団sv&lr=
- 2 http://www.google.co.jp/search?q=ie8+meta&lr=lang_ja&ie=utf-8&oe=utf-8&aq=t&rlz=1R1GGGL_ja___JP314&client=firefox-a
- 2 http://www.google.com/search?q=IE8+互換+meta&rls=com.microsoft:ja:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7GGLG
- 1 http://b.hatena.ne.jp/entrylist?of=29120


