Saturday, December 7, 2013

JavaFX animation on raspberry Pi

The exercise draw animation on Raspberry Pi base on user input. What it can do:
  • Load image from internet
  • Detect mouse action to draw the path
  • The image will run following the path

Please note that the JavaFX code have not handle the exit case. So if you run on local console, you cannot exit the program.

The program is developed on Windows 8 with Netbeans. The original Java code is copied from Java-Buddy. Modified as:
package javafx_path;

import javafx.animation.PathTransition;
import javafx.animation.PathTransitionBuilder;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @org java-buddy.blogspot.com
 * @web helloraspberrypi.blogspot.com
 */
public class JavaFX_Path extends Application {

    PathTransition pathTransition;

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("java-buddy.blogspot.com");
        Group root = new Group();
        Scene scene = new Scene(root, 800, 600, Color.WHITE);

        //load image from internet
        final Image image1 = new Image("http://goo.gl/kYEQl");
        
        final ImageView imageView = new ImageView();
        imageView.setImage(image1);

        final Path path = new Path();
        path.setStrokeWidth(1);
        path.setStroke(Color.BLACK);

        //Mouse button pressed 
        //- clear path and start from the current X, Y.
        scene.onMousePressedProperty().set(
                new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                path.getElements().clear();
                path.getElements().add(
                        new MoveTo(event.getX(), event.getY()));
            }
        });

        //Mouse dragged - add current point.
        scene.onMouseDraggedProperty().set(
                new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                path.getElements().add(
                        new LineTo(event.getX(), event.getY()));
            }
        });

        //Mouse button released,  finish path.
        scene.onMouseReleasedProperty().set(
                new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                pathTransition = PathTransitionBuilder.create()
                        .node(imageView)
                        .path(path)
                        .duration(Duration.millis(5000))
                        .orientation(PathTransition.OrientationType
                                .ORTHOGONAL_TO_TANGENT)
                        .cycleCount(1)
                        .build();

                pathTransition.play();
            }
        });

        root.getChildren().add(imageView);
        root.getChildren().add(path);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Copy the generated jar,JavaFX_Path.jar, to Raspberry Pi. Then run it in text console by enter:
$ java -jar JavaFX_Path.jar



Related:
- Run JavaFX on Raspberry Pi

No comments: