About Me

My photo
Author of Groovy modules: GBench, GProf, Co-author of a Java book パーフェクトJava, Game Developer at GREE

Sunday, September 2, 2012

SmileyFX: Smiley for JavaFX

Smiley ☺ is often used in graphical programs. SmileyFX is a library that allows to easily create Smiley in JavaFX. You can make a Smiley just as follows using SmileyFX:

Node smiley = SmileyFX.smiley(40);

That's it! The code creates a 40 pixel diameter (20x20xπ pixels) Smiley. Here is a perfect example in JavaFX and GroovyFX:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.stage.Stage;
import smileyfx.SmileyFX;


public class JavaFXDemo extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Node smiley = SmileyFX.smiley(40);
        Scene canvas = SceneBuilder.create()
            .root(new Group(smiley))
            .width(160)
            .height(160)
            .build(); 
        
        // center align
        smiley.layoutXProperty().bind(
            canvas.widthProperty()
            .subtract(smiley.getLayoutBounds().getWidth())
            .divide(2));
        smiley.layoutYProperty().bind(
            canvas.heightProperty()
            .subtract(smiley.getLayoutBounds().getHeight())
            .divide(2));
        
        // scale by window resize
        smiley.scaleXProperty().bind(
            canvas.widthProperty()
            .divide(canvas.getWidth()));
        smiley.scaleYProperty().bind(
            canvas.heightProperty()
            .divide(canvas.getHeight()));
        
        stage.setScene(canvas);
        stage.setTitle("Smiley FX");
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }

}
import static groovyx.javafx.GroovyFX.start
import static smileyfx.SmileyFX.smiley

start {
    stage(title: "Smiley FX", visible: true) {
        scene(id: "canvas", width: 160, height: 160) {
            node(smiley(40)).with {
                // center align
                layoutXProperty().bind((canvas.width() - layoutBounds.width)/2)
                layoutYProperty().bind((canvas.height() - layoutBounds.height)/2)
                // scale by window resize
                scaleXProperty().bind(canvas.width()/canvas.width)
                scaleYProperty().bind(canvas.height()/canvas.height)
            }
        }
    }
}

SmileyFX is available in here. You can use it by the follow steps:

  1. git clone https://code.google.com/p/smileyfx/
  2. cd smileyfx && ant dist

1 comment: