vendredi 22 juillet 2011

Duke Anim in JavaFX 2.0 (b36)

This post is just the porting of DukeAnim in JavaFX 2.0 b36.

I tried to have the same structure (UI and Model in different classes) that I had for Duke Anim in JavaFX Script.
The only true change is in the code of the event action for the KeyFrame.
The KeyValue is now immutable and the Set of KeyValues in the KeyFrame is also immutable, so I have to create one new KeyFrame with its news KeyValues, in every event action
.

DukeAnim.java
package dukeanim;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.builders.GroupBuilder;
import javafx.builders.ImageViewBuilder;
import javafx.builders.SceneBuilder;
import javafx.builders.TimelineBuilder;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.util.Duration;

public class DukeAnim extends Application {
    
    public static void main(String[] args) {
        Application.launch(DukeAnim.class, args);
    }
    
    @Override
    public void start(Stage stage) {
        
        DukeAnimModel dukeAnimModel  = new DukeAnimModel();
        dukeAnimModel.timeline.play();
        
        stage.setTitle("DukeAnim JavaFX 2.0(b36)");
        
        ImageView imageView;
        
        Scene scene =  SceneBuilder.create()
            .width(240)
            .height(320)
            .root(GroupBuilder.create()
                .children(
                    imageView = ImageViewBuilder.create()
                        .image(new Image(
                            DukeAnim.class.getResourceAsStream("images/duke.png")
                            )
                         )                        
                    .build()
                )
                .build())
            .build();

        imageView.xProperty().bind(dukeAnimModel.x);
        imageView.yProperty().bind(dukeAnimModel.y); 
                
        stage.setScene(scene);                
        stage.setVisible(true);       
    }
}

class DukeAnimModel {

    private EventHandler action = new EventHandler<ActionEvent>() {

        public void handle(ActionEvent t) {
            System.out.println("x : " + x);
            System.out.println("y : " + y);

            int xTarget = (new Double(java.lang.Math.random() * 240 + 1)).intValue();
            int yTarget = (new Double(java.lang.Math.random() * 240 + 1)).intValue();

            System.out.println("xTarget : " + xTarget);
            System.out.println("xTarget : " + yTarget);

            KeyFrame kf1s = buildKeyFrame(xTarget, yTarget);

            timeline.stop();
            timeline.getKeyFrames().remove(0);
            timeline.getKeyFrames().add(kf1s);
            timeline.play();

        }
    };

    private KeyFrame buildKeyFrame(int xTarget, int yTarget) {

        Duration t1 = Duration.valueOf(1000);
        KeyValue kvx1s = new KeyValue(x, xTarget, Interpolator.SPLINE(0, 0.5, 0.5, 1));
        KeyValue kvy1s = new KeyValue(y, yTarget, Interpolator.SPLINE(0, 0.5, 0.5, 1));
        KeyFrame kf1s = new KeyFrame(t1, action, kvx1s, kvy1s);

        return kf1s;
    }
    
    public IntegerProperty x = new IntegerProperty(0);
    public IntegerProperty y = new IntegerProperty(0); 
   
    public Timeline timeline = TimelineBuilder.create()
            .autoReverse(true)
            .cycleCount(Timeline.INDEFINITE)
            .keyFrames(               
                buildKeyFrame(0,0)
                )
            .build();      
     
}
Get the NetBeans project
Build it and run it!


Aucun commentaire: