JavaFX Type Casting

I have been playing a little bit more with JavaFX. Unfortunately the language reference isn’t particularly complete, and it turns out that type casting in JavaFX is via the as keyword. (Thanks Andres Almiray for the tip!)

For example,

var myWidth = (e.node as Rectangle).width

Incidentally, the performance of the drop shadow effect leaves much to be desired, especially if the shape is sufficiently large. During animation, the effects should be cached and preference should be given to inaccurate but speedy rendering. For instance, during rendering, the effect should be timed and if it is too slow, then a cached bitmap should be used.

The drop shadow is a rather important visual effect, especially given that Windows XP has been decorating all their windows with a shadow for 6 years, I hope Sun doesn’t drop the ball on this in JavaFX 1.0.

import javafx.scene.image.*;
import javafx.application.*;
import javafx.animation.*;
import javafx.scene.effect.*;
import javafx.scene.paint.*;
import javafx.scene.geometry.*;
import javafx.scene.text.Text;
import javafx.input.*;

var imX:Number=0;
var WIDTH:Number=400;

Frame {
    closeAction:function() { java.lang.System.exit(1); }
    stage: Stage {
        fill:Color.web('#333333')
        content: [
            Text { content:"Click on rectangle to make it smaller and observe increased frame rate" x:10 y:10 fill:Color.ORANGE},
            Circle { cache:true centerX:400 centerY:100 radius:80 fill:Color.YELLOW },
            Rectangle {
                cache:true
                x:bind imX y:100 arcWidth:20 arcHeight:20
                width:WIDTH height:WIDTH*0.75
                fill:Color.web('#6699ff')
                effect: DropShadow { radius:20 color:Color.web('#111111') offsetX:10 offsetY:10 }
                onMouseClicked: function (e:MouseEvent) {
                    (e.node as Rectangle).width  = (e.node as Rectangle).width  * 0.70;
                    (e.node as Rectangle).height = (e.node as Rectangle).height * 0.70;
                }
            },
        ]
    }
    visible:true height:768 width:1024
    title:"DropShadow Test"
}
Timeline {
    repeatCount:Timeline.INDEFINITE autoReverse:true
    keyFrames: [
        KeyFrame {
            time: 3s
            values: { imX => 800 tween Interpolator.EASEBOTH }
        }
    ]
}.start();

About this entry