显示标签为“新新技术”的博文。显示所有博文
显示标签为“新新技术”的博文。显示所有博文

2009-05-01

JavaFX GUI Learning: Task Learning

Learning from practice!




This article is the note of my learning of JavaFX under the offical javaFX website tutorial:
http://www.javafx.com/docs/tutorials/mediabrowser/index.jsp

Task 1: Loading and Displaying an Image


Graph Programming Model :

  • Stage is the top level container for JavaFX program.
  • Scene is a drawing surface for graphical content and a container that holds the scene graph nodes. It can be added to Stage, and JavaFX renders everything on a scene.
  • CustomNode is a subclass of javafx.scene.Node and created by overriding the create function. It can have one child node.
  • Group is a sequence of child nodes.

  • Node is an element in a scene graph, and javafx.scene.Node is an abstract class. The following visual objects are examples of javafx.scene.Node implementation: javafx.scene.image.ImageView, javafx.scene.media.Mediaview, javafx.ext.swing.*, javafx.scene.shape.*, and javafx.scene.text.Text. These are leaf nodes, which cannot have a child element.

By the tutorials of SUN, it supply a architecture of this program:




In our program, main.fx is the main class file and the first class to be built, we build a stage and add a scene into it.

You can download the netbeans project file to have a detail looking of this project

http://www.javafx.com/docs/tutorials/mediabrowser/dist/module01-task01_nb.zip

The code of offical demo of task 1 is a little confusing to beginner, So I have change some code and comment so that it is easy for my self to understand, if you need, you can download it from http://ulysess.ys168.com/


After the task 1, we have a simply program that can scale-up a image by user's click.


Task 2 :will create a image wall.


So it is a good habbit to define a action by code like this:

package var fullView : function(metaData : MetaData, placeholder: Image) : Void;

object does not need to know how to do fullView, it let other object which will arrange its to do it.

Task 3: Adding a Expansion effect

Expansion effect will scale up you thumbnail 25% bigger while find your cursor is above it

In order to implementation that, we should make the program know how to center the thumbnail after scaled, and handing the expansion such that it does not make the wall resize.

We just need change thumbnail.fx class, put image into a boundingRect and use imageView to painting the thumbnail, and finally some mouse event, that is what we need.

Offical project can be download in http://www.javafx.com/docs/tutorials/mediabrowser/dist/module01-task03_nb.zip



Task 4:Building a Animation Effect

A program provide scale the image must have the basic function of zoom in and zoom out. We need timeline, key frame and interpolators to implement that functions.

  • Timeline is an object that contains a list of key frames, together with a function to control the animation, which might be to start it, stop it, and repeat it.

  • KeyFrame describes a set of end states of various properties (values) of objects at a certain time instant relative to the start of the animation, together with interpolation specifications to calculate the in-between values relative to the previous frame.
  • KeyValue class defines a target, which is a reference to an object to animate, a value for the object to take on at the KeyFrame time, and an Interpolator.
  • The type of interpolation is defined with a KeyValue so that different interpolation types can be used for each KeyValue in a KeyFrame and at different KeyFrame times. Custom interpolators can be written, but the following simple interpolators are provided for animating numeric values:

  • Interpolator.LINEAR - (The default) uses simple linear interpolation.

  • Interpolator.EASEOUT - Start changing the value slowly after the KeyFrame.

  • Interpolator.EASIN - Slow the rate of change as the KeyFrame is reached.
  • Interpolator.EASEBOTH - Smooth the rate of change through the KeyFrame.

  • A shorthand syntax is provided for defining simple KeyFrames.
    at ( &;lttime&;gt ) {
    &;lttarget&;gt =&;gt &;ltvalue&;gt tween &;ltinterpolator&;gt;
    }

    Well, does it looks a little familar? Yes, if you have a basic knowledge about FLASH, It just about the same concept. After you know the knowledge It's a easy task to do this.

    Let us look at the architecture:

    There are two new operator in the code, the => operator provides a literal constructor for a list of key values. The tween operator is a literal constructor for an interpolated value.

    One thing should note in this section is that the interpolator can't be used at will other than it can result your animation play in unwanted way!

    Task 5: Scroll Controller Feature

    We have lots of photos and obviously it can not displayed in one screen, so we need scroll contorller to navigate to others thumbnails.


    In this task we need a new class ScrollControl and add two function in wall.fx

    JavaFX Script Learning: Basic Knowledge

    Declaring Variables:
    There are two type variables in JavaFX, var or def, we can use 'var' and 'def' keyword to declare varibles. the difference between the two is that var may be assigned new value while def not, so def is a little like final variables in Java.
    In JavaFX we can do not explicitly specify variables as its date type, compiler can figure out what is the data types you need, this called type inference.

    Defining and Invoking Functions:
    a basic format of function define:
    function add() { }
    a function with arguments:
    function add(argOne: Integer, argTwo: Integer) {}
    a function with returnvalue of Integer:
    function add() : Integer { var resule = 11; return resule;}
    Note that if no return value is specified, a function returns Void by default.
    Accessing Command-Line Arguments:
    function run(args : String[]) {}
    Note that run function is special that serves as the script's main entry point.
    Invoke a function just like invoke a method in Java.

    Using Object:
    def myAddress = Address { street: "I don't know"; }
    this line code create a Object of Address and assign the value of street instance variable of Address.

    Data Types:
    In JavaFX there are 6 build-in data types: String, Number and Integer, Boolean, Duration, Void, Null.
    String can be declared by single or double quotes.
    Number is floating-point number while Integer is integer number.
    Duriation type represents a fixed unit of time, it should be notated with time literals for example:
    5ms //5 milliseconds
    5s //5 seconds
    5m //5 minutes
    5h //5 hour


    Void is used toe indicate that a function does not return any value.
    null i a special value used to indicate a missing normal value, null is not the same as zero or empty string. you can use 'null' keyword to comparing if its null, in a word, its like null in java.


    Sequences in JavaFX:
    Sequences should be declared in square brackets[] ,such as


    def weekDays: String[] = ["mon","tue","wed","thu","fri"];
    def days : [weekDays, ["sat","sun"]];
    def nums= [1..10];
    //this declared a sequence contains numbers from 1 to 10 with step 1
    def numg= [10..1 step -1];
    //this declared a sequence contains number from 10 to 1 with step -1
    //Note that if create a descending range the second value should less
    // than first
    def nums2 = nums[ n | n &;lt 5]
    //this will create a sequence with the items from mums where value
    //less than 5
    def size =sizeof numg;
    // this will get the size of numg
    def weekend = days[5..6];
    //this will create a sequence with the items of days 6 to 7
    def weekend2 = days[5..];
    //this will create a sequence with the items of days 6 to the end
    //of days
    def weekdays2 = days[0..&;gt5];
    //this will create a sequence with the items of days 1 to 5
    def days2 = days[0..&;gt];
    //this will create a sequence with all items from days,
    //but the last item of days will be excluded


    Operators in JavaFX:
    +-*/is same to Java
    mod get remainder
    not is inverts the balue of boolean
    and or is same to Java
    instanceof is same to Java


    Expressions in JavaFX:
    A block expressions surrounded by curly braces and must be sparated by semicolons. The VALUE of block expression is the value of the last expression. var and def are expressions.
    Look for expression:
    var nums= [1..10] ;
    for(num in nums) { println(nums); }

    2009-04-27

    JavaFX Script Learning: Data Binding and Triggers

    One of the most useful feature in JavaFX is its Data Binding and Triggers functions. According to the official tutorial, Data binding is the ability to create an immediate and direct relationship between tow variables, This allow two or more variables can automatically change when the other or others change. At the same time, A Replace Trigger is a block of code that is attached to a variable, which when the variable changes, the code is automatically executed. So let's begin here.
    Here is an example :


    var x = 0;
    def y = bind x;
    x=1;
    println(y); //y now equals 1
    x = 333;
    println(y); // y now is 333;

    That is a extreme sample example to show the use of 'bind' keyword, You can know in that example, y is binded to x, and its value is changed with x, that's the basic use of bind. Note that y is declared as a def because we don't want any thing change y by assigning a value to it.
    An other example:

    var myStreet= " No.2 Phoenix Garden"
    var myCity = "Xiamen"

    def address = bind Address {
    street : myStreet;
    city : myCity;
    };

    println({address.street});
    myStreet = "No.33 NoWhere"
    println({address.street});


    In this example the result of execute is :
    No.2 Phoenix Garden
    No.33 NoWhere

    So, the conclusion is we can bind a variable with the value of a bound expression, which can be a basic type, an object, the outcome of a function, or the outcome of an expression. But in this example we must note that the changing of myStreet actually causes a new Address object to be created and then re-assigned to the address variable. If you don't want this happen, you can change the definition like this:

    def address = bind Address{
    street: bind myStreet;
    city : myCity;
    }

    By this, you can change street without create a new object, and the bind in def line can be omited.

    In next step, let us look at Binding and Functions.

    Here is an example :

    var scale = 1.0;

    bound function makePoint(xPos : Number, yPos : Nubmer) : Point {
    Point {
    x: xPos * scale;
    y: yPos * scale;
    }
    }

    class Point {
    var x : Number;
    var y : Number;
    }

    var myX = 5.0;
    var myY = 9.0;
    def pt = bind makePoint(myX, myY);
    println(pt.x);

    myX *=2;
    println(pt.x);

    scale = 2.0;
    println(pt.x);

    At the head of this code, we announce a bound function by 'bound' keyword, and then we use our own myX and myY number to create a Point by that bound function, note we use bind keyword before the invocation of makePoint function, so pt binded to the outcome of makePoint function.
    The result of executing is :
     
    5.0
    10.0
    20.0

    But if we remove the bound keyword from makePoint function(it became a non-bound function), the result would be:

    5.0
    10.0
    10.0

    The different between these two situation is a bound function will get re-invoked when every variable related to the function changed while the non-bound function only do that when one of their arguments change. And we MUST NOTE that bound function and bind keyword is work together.


    In next step, let us look at Binding with Sequences:

    Here is an example:

    var q1 = [1..10];
    def q2 = bind for (item in q1) item *8;
    printSeqs();
    insert 11 into q1;

    function printSeqs() {
    println("First Sequence:");
    for( i in q1) { println(i) ; }
    println("Second Sequence:");
    for( i in q2) { println(i) ; }
    }

    You can guessed the result is first sequence is 1 to 10 with step of 1 and the second sequence is 8 to 80 with step of 8.
    In this example, we bind two sequence by placing the bind before the for. For more understanding, we can try to change one or some items of q1, you can find the corresponding items q2 will automatically changed and others will not be affect, like we do in the example we insert a item 11 into the end of q1 and q2 automatically generated a item 88 at the end of itself.

    Finally, let us look at the Replace Triggers.
    It is arbitrary blocks of code that attach to variables and execute whenever the variable's value changes. Here is an example:

    var test = "test" on replace oldValue {
    println(" value has changed, old value is { oldValue}");
    }

    test = "no";

    The trigger will fires two times, first is the initialized to "test" and the next is fires When the code "test= "no"; " executed, the program will report value has changed. NOTE that the oldValue store the value of test before the trigger was invoked.

    2008-11-12

    ZFS混合存储池特性浅析

      我对磁盘阵列相关的东西不是很熟,但是可惜的是前天正好遇到这个问题,亟需解决,无奈之下思考起了解决方案,但是可惜的是,我的查询zfs和raidz的一些情况的时候要不找到它的原理说明,要不找到配置方法,但是总觉得差什么,就是没办法把整个过程联通起来,最后发现,这些资料都缺乏对整体架构的解释。无奈的在unix-center中求助也没人回答,不过今天正好有人发了一帖倒解决的我的问题,下文即转自此帖:http://www.unix-center.net/bbs/viewthread.php?tid=7053&extra=page%3D1

    ------------------------------------------
    ZFS混合存储池特性浅析

      被称作“史上最后一个文件系统” 的ZFS,作为Solaris 10和OpenSolaris的新特性引进后,吸引了开源社区里无数人的关注,Linux之父Linus Torvalds公开表示对Solaris软件的ZFS(Zettabyte文件系统)特别感兴趣,苹果公司也宣称Mac OSX采用ZFS文件系统。从OpenSolaris 2008.05发布版开始,SUN在ZFS中引进了对Intel® High-Performance Solid State Drives (SSDs)的支持,提供混合存储池(Hybrid Storage Pools),本文将对此进行介绍。

    1. 什么是SSD?
      SSD即固态硬盘(solid state disk),SSD由控制单元和存储单元(FLASH芯片)组成,简单的说就是用固态电子存储芯片阵列而制成的硬盘。闪存设备记忆数据不需要电源,而且读写数据的速度非常快,就像 DRAM一样。随着全球电价上涨,以及海量数据存取速度的需求,SSD越来越被看好。
      以下是SSD、HDD的存取速度、单位造价和耗电量的对比,IOPS 即Input/Output Operations PerSecond。

    • SSD:7000 write IOPS, 35000 read IOPS, $2/GB ,15-20w
    • HDD:180 write IOPS, 320 read IOPS ,$30/GB,2.5w
    2. 改进存储系统的动机
      计算机系统设计的黄金法则——加快经常性事件(make the common case fast),道理十分明显,使经常性事件的处理速度加快能明显提高整个系统的性能。遵循大师们提出的这一准则,RISC、RAID等大批技术横空出世。前辈们屡试不爽的准则,在ZFS的创新过程中,又一次得到成功验证。
      从下图可以看出,随着多核技术的发展,特别是服务器端多核技术的广泛应用,磁盘存取速度的增长远远跟不上处理器处理速度的增长,整个系统的性能越来越拖累于慢速的磁盘。同时服务器端海里数据存取,产生的大量磁盘操作已经成了经常性事件。磁盘访问已经成为系统的瓶颈,而且数据中心大量磁盘产生的电费也是一笔不菲的成本。


    SUN的ZFS+SSD解决方案

    解决上述问题有以下一些方案

    • 增加内存缓存一些数据来提高数据存取性能,但是存储容量的增长速度远高于内存容量的增长,并且代价十分高昂。
    • 利用闪存比DRAM便宜且存取速度相当,将闪存设备作为另一层级的存储器引进数据中心。但这会出现转换成本或操作成本,使数据中心产生新的成本开支,面临新的管理纷争。
      可见以上方案均不是很合理,那么如何实现高性能、低能耗、对用户和操作者透明的解决方案呢?答案就是SUN ZFS+SSD混合存储池。

      ZFS+SSD的体系结构如下图,在系统中,使用少量高速SSD作为内存与磁盘的缓存,将经常使用的数据放到快速的缓存,加快了访问速度,同时保留了HDD的大容量存储能力。同时ZFS对SSD进行了无缝整合,可以把SSD 作为文件系统的二级缓存(L2 ARC)以及ZIL(ZFS Intent Log),自动优化系统充分利用快速SSD提供系统读写吞吐率。


    ZFS+SSD的体系结构

    1. Read/Write Cache Pool:ZFS利用主存和SSD作为自动伸缩缓存(Adaptive Replacement Cache),并能检测访问模式
    2. ZIL(ZFS Intent Log) Pool:ZFS一般使用NVRAM,SSD(Solid State Disk)作为 ZIL存储池。ZFS是事务型文件系统,对于同步写操作,ZFS为系统中每一个ZFS文件系统维护一个ZIL。同步写操作的数据会先写入ZIL,并且会把磁盘的write cache的数据同步到磁盘上,然后应用的写操作返回。当文件提交命令发生时,ZFS会把ZIL里该文件的数据同步到磁盘上。
    3. DISK Pool:ZFS利用disk pool为文件系统自动分配HDD空间
    3. 总结
    ZFS文件系统是对传统文件系统的一次革命性的创新设计,ZFS解决了文件系统的完整性、安全性和可伸缩性以及管理困难等重要难题。ZFS文件系统是世界上第一个128位的文件系统,其存储容量和文件数量几乎只受硬件的限制,增加了混合存储池后ZFS功能将更强大,心动了吧,赶紧去装个OpenSolaris体验下吧!
    参考文献:

    [1] Solaris zfssolutionbrief
    http://www.sun.com/software/solaris/pdf/solariszfs_solutionbrief.pdf
    [2] Sun Intel flash pitch April 08, Bill Moore
    [3] http://blogs.sun.com/jonathan_zh/date/20080620, Jonathan(Sun CEO)

    2007-12-08

    IBM用光信号连接CPU的多核心

      IBM目前正在研究一项新的技术,据称这项技术将能够让一颗处理器中的多个核心使用光信号相连接,而不是传统的电信号。
      该装置被称之为硅Mach-Zehnder型电光调制器,把电信号转换为脉冲光信号。这项技术的技术重点是,IBM的电光调制器比当今其他实验室使用的调制器小100倍。IBM最终的目标是把这个调制器整合至芯片当中。
      以下的图片解释了这个电光调制器的工作原理。黄色小点代表电脉冲,当电脉冲到达调制器之后,该调制器会不断的放出光脉冲以对应电脉冲。



      上个世纪90年代初,数家公司曾聚集在一起拿出自己的技术,以实现将电信号转换为光信号的过程。这些公司包括,Intel、Primarion、Luxtera、IBM。事实上当初这样做的目的是为了解决电子信号的热辐射问题。(目前有关这方面的研究称之为硅光子学与光电子学)
    何时把装备了光电子电路的芯片推向市场仍是一个悬而未决的问题。但是目前的这些原型产品表明,这项研究正在朝向良好的方向发展。