First steps with JavaFX

Recently I visited a presentation about JavaFX and I got the impression, that this might be something which might be useful for my OpenPatrician project. Therefore I added a task to explore different aspects need for the game. In this first part I present two basic examples.

The first is as always Hello World. A simple window with a button. When clicking on the button, the „Hello World“ string will be outputted on the console:

HelloWorldJavaFX

 

package javafxtest;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * Hello World example.
 * @author andi
 */
public class JavaFXTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
//        launch(args);
        OverlayTest.launch(args);
    }
}

The second example is a bit more exiting. This is the first step toward the goal of using JavaFX in my rendering framework. The framework uses image and overlays other images on top.

There are two features in JavaFX for this. The first is that the base layout is a StackLayout, the second is the use of a Canvas, where you can have an image or composition outside of JavaFX control.

The main question here is when having a Canvas as background and then using a second Canvas as overlay with an image with transparent areas, does the canvas on top hide the one behind or are the transparent parts of the image also transparent in the canvas. As it turns out they are:

StaticOverlay

package javafxtest;

import java.io.File;
import java.io.FileInputStream;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author andi
 */
public class OverlayTest extends Application {

    @Override
    public void start(Stage stage) throws Exception {
         StackPane root = new StackPane();
        double width = 600;
        double height = 500;
        Canvas background = new Canvas(width, height);
        GraphicsContext context = background.getGraphicsContext2D();
        File f = new File("/home/andi/Pictures/Adam.jpg");
        Image image = new Image(new FileInputStream(f));
        context.drawImage(image, 0, 0);

        root.getChildren().add(background);

        Canvas animation = new Canvas(width, height);
        GraphicsContext context2 = animation.getGraphicsContext2D();

        File overlay = new File("/home/andi/Pictures/kajak1.png");
        Image kajak = new Image(new FileInputStream(overlay));
        context2.drawImage(kajak, 36, 56);

        root.getChildren().add(animation);

        Scene scene = new Scene(root, width, height);

        stage.setTitle("Overlay Test");
        stage.setScene(scene);
        stage.show();
    }
    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }    
}

The next steps will be:

  • Animate the overlay image by moving the image and using different images
  • Use ImageView instead of Canvas, as a canvas cannot be hardware accelerated by JavaFX (the whole canvas is redrawn on each rendering)

Ein Gedanke zu „First steps with JavaFX“

Schreibe einen Kommentar