Event-Driven Programming and Animations
Event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs. JavaFX is heavily based on event-driven programming.
Handling Events in JavaFX
In JavaFX, an event represents an action that has occurred, such as a button click or a key press. You can handle these events by creating an event handler.
Example: Handling a Button Click
Here's an example of how to handle a button click in JavaFX:
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;
public class ButtonClickExample extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Button Click Example");
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);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Explanation
Button: AButtonis a control that can be clicked.setOnAction(): This method sets the action to be performed when the button is clicked.EventHandler<ActionEvent>: This is an interface that you can implement to handle events. Thehandle()method is called when the event occurs.- Lambda Expressions: You can also use a lambda expression to make the code more concise:
btn.setOnAction(event -> System.out.println("Hello World!"));
Animations in JavaFX
JavaFX provides a rich set of APIs for creating animations. You can animate properties of nodes, such as their position, size, and color.
Example: A Simple Fade Transition
Here's an example of how to create a simple fade transition in JavaFX:
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FadeTransitionExample extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Fade Transition Example");
Rectangle rect = new Rectangle(100, 100, 100, 100);
rect.setFill(Color.BLUE);
FadeTransition ft = new FadeTransition(Duration.millis(3000), rect);
ft.setFromValue(1.0);
ft.setToValue(0.1);
ft.setCycleCount(4);
ft.setAutoReverse(true);
ft.play();
StackPane root = new StackPane();
root.getChildren().add(rect);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Explanation
FadeTransition: This class is used to create a fade animation.Duration.millis(3000): This sets the duration of the animation to 3000 milliseconds (3 seconds).setFromValue()andsetToValue(): These methods set the starting and ending opacity values for the animation.setCycleCount(): This sets the number of times the animation should be played.setAutoReverse(): This makes the animation reverse its direction at the end of each cycle.play(): This method starts the animation.