JavaFX Graphics

JavaFX is a powerful platform for creating desktop applications with rich user interfaces. One of its key features is the ability to create and manipulate graphics. In this article, we will explore some of the basic concepts and techniques for working with graphics in JavaFX.

Introduction to JavaFX Graphics

JavaFX provides a wide range of classes and methods for creating and manipulating graphics. At its core, JavaFX uses a scene graph model to represent the graphical elements of an application. A scene graph is a hierarchical structure of nodes, where each node represents a graphical element such as a shape, text, or image.

To get started with JavaFX graphics, you need to create a Scene object and set it as the content of a Stage object. The Stage represents the main window of your application, and the Scene represents the content displayed within the window. Here is an example of how to create a simple JavaFX application with a graphical element:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class GraphicsApp extends Application {
    @Override
    public void start(Stage primaryStage) {
        // Create a rectangle
        Rectangle rectangle = new Rectangle(100, 100, 200, 200);
        
        // Create a pane and add the rectangle to it
        Pane pane = new Pane();
        pane.getChildren().add(rectangle);
        
        // Create a scene and set the pane as its content
        Scene scene = new Scene(pane, 400, 400);
        
        // Set the scene as the content of the stage
        primaryStage.setScene(scene);
        
        // Show the stage
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

In this example, we create a Rectangle object with a width and height of 200 pixels and position it at coordinates (100, 100). We then create a Pane object, add the rectangle to it, and set the pane as the content of the scene. Finally, we set the scene as the content of the stage and show the stage.

Drawing Shapes

JavaFX provides a variety of shape classes, such as Rectangle, Circle, Ellipse, and Line, that you can use to draw different shapes on the screen. These shape classes inherit from the Shape class, which provides common properties and methods for manipulating shapes.

To draw a shape on the screen, you need to create an instance of the shape class and add it to a pane or another container. You can then customize the appearance of the shape by setting its properties, such as color, stroke width, and opacity. Here is an example of how to draw a circle on the screen:

Circle circle = new Circle(200, 200, 100);
circle.setFill(Color.RED);
circle.setStroke(Color.BLACK);
circle.setStrokeWidth(2);

In this example, we create a Circle object with a center at coordinates (200, 200) and a radius of 100 pixels. We then set the fill color to red, the stroke color to black, and the stroke width to 2 pixels.

Handling Events

JavaFX provides a powerful event handling framework that allows you to respond to user interactions with graphical elements. Events are generated when the user performs an action, such as clicking a button or moving the mouse, and you can write code to handle these events and perform the desired actions.

To handle events in JavaFX, you need to register event handlers with the graphical elements that you want to monitor for events. An event handler is a piece of code that is executed when a specific event occurs. Here is an example of how to handle a button click event:

Button button = new Button("Click me");
button.setOnAction(event -> {
    System.out.println("Button clicked");
});

In this example, we create a Button object with the text "Click me" and register an event handler using the setOnAction() method. The event handler is implemented as a lambda expression that simply prints a message to the console when the button is clicked.

Animation

JavaFX provides powerful support for creating animations, allowing you to add dynamic and interactive elements to your applications. You can animate various properties of graphical elements, such as position, size, color, and opacity, using keyframes and timelines.

To create an animation in JavaFX, you need to define a Timeline object and add KeyFrame objects to it. A KeyFrame represents a point in time and specifies the values of the animated properties at that time. The Timeline controls the timing and duration of the animation. Here is an example of how to animate the position of a circle:

Circle circle = new Circle(100, 100, 50);
TranslateTransition animation = new TranslateTransition(Duration.seconds(2), circle);
animation.setFromX(100);
animation.setToX(300);
animation.setAutoReverse(true);
animation.setCycleCount(Animation.INDEFINITE);
animation.play();

In this example, we create a Circle object and a TranslateTransition object. We set the duration of the animation to 2 seconds and specify the start and end positions of the circle