cooler javaFX kreations
Oracle recently released JavaFX 2, which is a rich client API that ditches the JavaFX language from JavaFX 1.x in favor of a pure Java library. And man, is it slick. It makes it very easy to implement rich, graphical applications or applets.
Inspired by @visualrinse’s Cooler Kreator, I created my own (greatly simplified) version of it, which I called “Cooler Kreations FX”. To use it, simply launch it, give it permission to run, type in a tag, and the program will consult Kuler for a random color scheme with the specified tag. Assuming, of course, that the Kuler API is working at the time (which, lately, hasn’t been very often…).
To create an application is as simple as extending the Application class.
public class CoolerKreations extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
stage.setScene(createScene());
}
private Scene createScene() {
....
}
}
You can use FXML to create your scenes declaratively, or write pure Java code to create your scenes programatically, or mix and match the two.
// declaratively, via FXML
Group root = FXMLLoader.load(getClass().getResource("my.fxml"));
Scene scene = new Scene(root, Color.BLACK);
// the FXML:
<VBox fx:controller="com.metatrope.controller.Controller" alignment="center" xmlns:fx="http://javafx.com/fxml" style="-fx-spacing: 5;">
<children>
<HBox alignment="center">
<children>
<Label text="Cooler Kreations" style="-fx-font: 42 Tahoma;" textFill="white" fx:id="title"/>
<Label text="FX" style="-fx-font-weight: bold; -fx-font-size: 42; -fx-font-style: italic;" textFill="white" fx:id="titlefx"/>
</children>
</HBox>
</children>
</VBox>
// programatically
Group root = new Group();
Text text = new Text();
text.setText("hi");
root.getChildren().add(text);
Scene scene = new Scene(root, Color.BLACK);
There are a lot of controls available. Visually, you can style your controls with CSS and there are plenty of effects you can use, such as drop shadows, reflections, blurs, and more. You can even chain them:
Bloom bloom = new Bloom(); bloom.setThreshold(2.0); BoxBlur blur = new BoxBlur(4 + randomInt(0, 5), 4 + randomInt(0, 5), 4 + randomInt(0, 6)); blur.setInput(bloom); // the output of bloom will be the input for this effect DropShadow ds = new DropShadow(); ds.setOffsetY(4.0f); ds.setOffsetX(4.0f); ds.setColor(Color.CORAL); ds.setInput(blur); // the output of blur will be the input for this effect // apply bloom AND blur AND a drop shadow to this text text.setEffect(ds);
JavaFX also makes it dead simple to animate controls. Simply bind a property and identify key points in the timeline, and the system will interpolate all the values in between. For example, here I tell JavaFX to rotate one of my nodes repeatedly:
// loop my node from 0 degrees to 360 degrees, completing one rotation every 15 seconds. // repeat ad infinitum. Timeline animation = new Timeline(); animation.getKeyFrames().addAll( new KeyFrame(Duration.ZERO, new KeyValue(node.rotateProperty(), 0d)), new KeyFrame(Duration.seconds(15.0), new KeyValue(node.rotateProperty(), 360d))); ); animation.setCycleCount(Animation.INDEFINITE); animation.play();
This is a huge step forward for rich client development in the Java space and a wonderful replacement for Swing. On modern JVMs, it even loads quickly in the browser; this wasn’t historically the case with JavaFX 1.x, or for applets in general, but nowadays Java applets start up almost as fast as Flash (and this will improve even more when Java becomes modularized in future versions.)
The source code can be viewed on my github account. You can test it out online as well (though only if you use Windows, unfortunately.) The result could look something like this:

