- by x32x01 ||
Metronome Using Translate Transition in JavaFX
If you’re learning JavaFX animations, this guide will teach you how to create a simple metronome using TranslateTransition.
This example demonstrates how to animate a circle back and forth with start, pause, resume, and stop buttons, perfect for beginners learning Java animation and GUI programming.
What is TranslateTransition?
TranslateTransition in JavaFX allows you to move nodes (like shapes, images, or buttons) from one position to another over a specified duration.
Key features:
JavaFX Metronome Code Explained
These imports include all necessary JavaFX classes for animations, shapes, buttons, and events.
2. Create the Circle Node
Circle circle = new Circle(100, 50, 4, Color.BLUE);
3. Create TranslateTransition Animation
4. Buttons for Controlling the Animation
5. Layout the GUI with HBox
6. Disable Buttons Based on Animation Status
Key Takeaways
Full Code Reference
You can copy and run the full JavaFX metronome code provided in this article to see the animation in action. Experiment by changing duration, distance, and interpolator for different effects! 
If you’re learning JavaFX animations, this guide will teach you how to create a simple metronome using TranslateTransition.
What is TranslateTransition?
TranslateTransition in JavaFX allows you to move nodes (like shapes, images, or buttons) from one position to another over a specified duration.Key features:
- fromX / toX: horizontal movement
- fromY / toY: vertical movement
- autoReverse: reverses movement automatically
- cycleCount: number of repetitions
- Interpolator: defines motion style (linear, ease-in, etc.)
JavaFX Metronome Code Explained
1. Imports and Setup
Java:
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration; 2. Create the Circle Node
Circle circle = new Circle(100, 50, 4, Color.BLUE);- centerX, centerY: initial position
- radius: size of the circle
- fill: color of the circle
3. Create TranslateTransition Animation
Java:
TranslateTransition anim = new TranslateTransition(Duration.millis(1000), circle);
anim.setFromX(0);
anim.setToX(200);
anim.setInterpolator(Interpolator.LINEAR);
anim.setAutoReverse(true);
anim.setCycleCount(Timeline.INDEFINITE); - Moves the circle from 0 to 200 pixels horizontally
- Linear motion ensures constant speed
- AutoReverse makes the circle move back and forth
- INDEFINITE cycle makes the metronome continuous
4. Buttons for Controlling the Animation
Java:
Button startButton = new Button("Start");
startButton.setOnAction(e -> anim.playFromStart());
Button pauseButton = new Button("Pause");
pauseButton.setOnAction(e -> anim.pause());
Button resumeButton = new Button("Resume");
resumeButton.setOnAction(e -> anim.play());
Button stopButton = new Button("Stop");
stopButton.setOnAction(e -> anim.stop()); - Start: begin the animation from the beginning
- Pause: temporarily pause animation
- Resume: continue from pause
- Stop: halt animation completely
5. Layout the GUI with HBox
Java:
HBox controls = new HBox(10, startButton, pauseButton, resumeButton, stopButton);
controls.setLayoutX(60);
controls.setLayoutY(420);
Group root = new Group(circle, controls);
Scene scene = new Scene(root, 400, 500);
stage.setScene(scene);
stage.setTitle("Metronome using TranslateTransition");
stage.show(); - HBox: arranges buttons in a row with spacing
- Group: adds circle and buttons to the scene
- Scene: defines the window size
6. Disable Buttons Based on Animation Status
Java:
startButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.STOPPED));
pauseButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.RUNNING));
resumeButton.disableProperty().bind(anim.statusProperty().isNotEqualTo(Animation.Status.PAUSED));
stopButton.disableProperty().bind(anim.statusProperty().isEqualTo(Animation.Status.STOPPED)); - Automatically enables/disables buttons based on animation state
- Prevents invalid actions (like pausing an already paused animation)
Key Takeaways
- TranslateTransition is perfect for pendulum or linear animations
- Combine it with buttons for interactive controls
- JavaFX interpolators let you create smooth and professional animations
- This metronome example is a great starting point for learning JavaFX GUI and animation techniques
Full Code Reference
You can copy and run the full JavaFX metronome code provided in this article to see the animation in action. Experiment by changing duration, distance, and interpolator for different effects! Last edited: