---
title: "JavaFx实现-渐变效果之一"
author: "mmdev"
date: "2007-12-18T10:43:00Z"
tags: []
slug: "javafx"
draft: false
lastmod: "2012-04-14T20:14:14Z"
id: 1486370
url: "https://mmdev.iteye.com/blog/1486370"
---

自从JavaOne大会推出JavaFX后，我一直比较关注这个项目，它是一种利用Java的脚本技术，功能上近似于Flash。 它的优势在于，Flash虽有很好的功能和普及度，但它永远不能交付Java类库的大部分功能，而 JavaFX可以。因为也给了JavaFX一个“可能性”上的生存空间。

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

但是由于JavaFX推出的太晚，兼之文档较少，很多人还是对其望而止步，干等下去也不是办法。所以我最近空闲时会写一些JavaFX的小例子以作练习之用。

![](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/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)classCircleTransitionextendsCompositeNode...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定参数项变量名：类型  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributer:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributed:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributew:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributeh:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributexp:Number;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)attributeimg:Image;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//为参数赋值  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)attributeCircleTransition.r=-45;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)attributeCircleTransition.d=16;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)attributeCircleTransition.w=320;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)attributeCircleTransition.h=240;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)attributeCircleTransition.xp=-w;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//注入过滤器参数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)functionCircleTransition.composeNode()=Group...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)content:[Clip...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//注入shape  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)shape:Rect...{x:0,y:0,width:w,height:h},  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//设定偏移  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)transform:translate(10,40),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//注入Group  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)content:Group...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//加载ImageView用以显示图像  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)content:[ImageView...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)transform:translate(-60,-30),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)image:this.img,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//分段绘制图像  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)},Subtract...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)shape1:Rect...{x:0,y:0,width:w\*4,height:h\*4},  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)fill:orange,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//bind  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)transform:bind\[rotate(r,0,h),translate(-w+xp,0)],  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)shape2:Union...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)content:\[foreach(jin\[0..w/d],iin\[0..h/d\*2])Circle...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)radius:j,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cx:w-j\*d,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cy:i\*d,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)},Rect...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)x:-w,y:0,width:w+w/2,height:h\*2,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}],  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)},View...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)content:GroupPanel...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)varrow=Row...{alignment:BASELINE}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)varcolumn1=Column...{}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)varcolumn2=Column...{}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//鼠标设为默认  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cursor:DEFAULT  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rows:\[row]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)columns:\[column1,column2]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)content:[SimpleLabel...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)row:row  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)column:column1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)text:"操作:"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)},Button...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)row:row  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)column:column2  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)opaque:false  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)mnemonic:T  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)text:"变更图像"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)action:operation()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)xp=\[0,d..w\*2]dur2000;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}]  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.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-图像渐变效果1"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)width:350  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)height:350  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)content:Canvas...{content:CircleTransition...{img:...{url:"image.jpg"}}}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)centerOnScreen:true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)visible:true  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)};

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

效果图：  
![](http://p.blog.csdn.net/images/p_blog_csdn_net/cping1982/javafx0.gif)

第一次写代码的感觉就是效率太慢……

由解释到执行的效率比Swing还不能令人忍受（当然，有代码优化的余地，但还是太慢）

且抛去效率问题不提，桌面JavaFX现在还有一个比较关键的问题，那就是如何部署。对于从前没有安装过JRE的用户来说，JRE体积太大，而且现在从浏览器上进行按需安装（如使用Java Start Web等）的用户体验也不是特别好。

目前Sun的官方解决思路是为JRE引入一种新型的部署模型，初次下载时体积非常小，只有在需要时才下载其他的JRE组件。但是具体的效果如何，我们还需拭目以待。

PS:如果Windows能普及jre该有多好啊……