---
title: "JavaFx实现(2)-随机图形绘制"
author: "mmdev"
date: "2007-12-19T10:11:00Z"
tags: []
slug: "javafx-2"
draft: false
lastmod: "2012-04-14T20:14:05Z"
id: 1486367
url: "https://mmdev.iteye.com/blog/1486367"
---

JavaFX属于脚本语言，并且也采取了弱类型进行数据处理，具体对应关系如下：

JavaFX Java `String` `java.lang.String` `Boolean` `java.lang.Boolean` `Number` `java.lang.Number` `Integer` `byte,short,int,long,BigInteger`

例子：

// var s = "Hello";  
// s.toUpperCase(); // 结果 "HELLO";  
// s.substring(1); // 结果 "ello";  
// var n = 1.5;  
// n.intValue(); // 结果 1  
// (1.5).intValue(); // 结果 1  
// s.substring(n); // 结果 "ello"  
// var b = true;  
// b instanceof Boolean; // 结果 true

这种方式虽然减少了类型转换的烦恼……但也影响解释速度，个人不是太认可……(而且在JavaFX中，很多还是需要转换类型的……)

JavaFX脚本为多种多样的操作提供了声明式、无中间程序逻辑的语法，这些操作包括创建2D动画、设置属性或者声明在模式和视图对象之间的绑定依赖关系。

JavaFX以operation代替了Function，换汤不换药，无视中……

还有‘+’已无法在JavaFX中连接字符串用，需要写成如"Hello ".concat("World")的形式。

大体上先想起这么多，继续写个例子：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*\*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Title:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Description:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Copyright:Copyright(c)2007  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*Company:LoonFramework  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*&lt;/p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@authorchenpeng  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@email：ceponline@yahoo.com.cn  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*@version0.1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavafx.ui.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjavafx.ui.canvas.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.util.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.lang.System;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.lang.Thread;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//JavaFXJava  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//Stringjava.lang.String  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//Booleanjava.lang.Boolean  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//Numberjava.lang.Number  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//Integerbyte,short,int,long,BigInteger  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//vars="Hello";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//s.toUpperCase();//结果"HELLO";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//s.substring(1);//结果"ello";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//varn=1.5;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//n.intValue();//结果1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//(1.5).intValue();//结果1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//s.substring(n);//结果"ello"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//varb=true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//binstanceofBoolean;//结果true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)classCircleModel...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributex:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributey:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributer:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributec:Color;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)classCircleDrawextendsCompositeNode...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributetags:CircleModel\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//javafx的操作和c++类似需要提前声明,否则会\[Nosuchfunction]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)operationmakeing();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//实现composeNode操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)operationCircleDraw.composeNode()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)returnGroup...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)content:[  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//遍历tags  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)foreach(tintags)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)Circle...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//绑定  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cx:bindt.x  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cy:bindt.y  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)radius:bindt.r  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fill:bindt.c  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//实现makeing操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)operationCircleDraw.makeing()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)varrand=newRandom();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(unitintervalunitindur-1)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)for(tintags)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)if(t.r==0andrand.nextInt(100)&gt;95)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//产生随机坐标  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.x=rand.nextInt(400)+40;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.y=rand.nextInt(400)+40;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.r=4;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//变换color  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.c=rgba:Color(  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rand.nextInt(256),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rand.nextInt(256),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rand.nextInt(256),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rand.nextInt(128)+64  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//类Basic语法也成……  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)t.r=if(t.r&gt;200)or(t.r==0)then0elset.r+4;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//延迟  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Thread.sleep(50);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)vartagContainer=CircleDraw...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)tags:foreach(iin\[0..15])CircleModel...{}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)Frame...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)title:"JavaFX-随机画圆"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)centerOnScreen:true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//javafx也可使用通常java类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)onClose:operation()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)width:430  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)height:430  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)resizable:false  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)content:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//背景设置  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)Canvas...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)background:black  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)content:tagContainer  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)visible:true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//执行操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)tagContainer.makeing();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/javafx1.gif)