I came across a nice post called
JavaFX 2.0: Implement Fade In/Fade Out Animation. Since I've been poking around with JavaFX with Scala, I thought I'd take a few minutes to write the same example using
ScalaFX, a Scala UI DSL that sits on top of JavaFX.
package hohonuuli.sfx
import javafx.event.{ActionEvent, EventHandler}
import javafx.scene.control.Button
import javafx.scene.input.MouseEvent
import scalafx.animation.FadeTransition
import scalafx.application.JFXApp
import scalafx.Includes._
import scalafx.scene.layout.StackPane
import scalafx.scene.Scene
import scalafx.stage.Stage
import scalafx.util.Duration
object FadeInOut extends JFXApp {
stage = new Stage {
title = "Fade Button In and Out"
width = 300
height = 250
}
private val btn = new Button {
setText("Say 'Hello World'")
setOnAction(new EventHandler[ActionEvent] {
def handle(event: ActionEvent) {
println("Hello World")
}
})
}
private val theRoot = new StackPane {
children += btn
}
private val theScene = new Scene {
root = theRoot
onMouseEntered = new EventHandler[MouseEvent] {
def handle(mouseEvent: MouseEvent) {
val fadeTransition = new FadeTransition {
duration = Duration(500)
node = btn
fromValue = 0
toValue = 1
}
fadeTransition.play()
}
}
onMouseExited = new EventHandler[MouseEvent] {
def handle(mouseEvent: MouseEvent) {
val fadeTransition = new FadeTransition {
duration = Duration(500)
node = btn
fromValue = 1
toValue = 0
}
fadeTransition.play()
}
}
}
stage.scene = theScene
}
p.s. If you check out the
source code for ScalaFX and want to compile and use it, here's the commands to build and install it in your local Maven repository (i.e. ~/.m2/repository):
cd scalafx
sbt clean compile package make-pom package-src
installjar.sh
0 comments:
Post a Comment