Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created August 13, 2021 12:11
Show Gist options
  • Save Da9el00/8141d962ae4d6a3670963181cb0f7c4e to your computer and use it in GitHub Desktop.
Save Da9el00/8141d962ae4d6a3670963181cb0f7c4e to your computer and use it in GitHub Desktop.
JavaFX with Scne Builder - Bouncing Ball
package sample;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Bounds;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private AnchorPane scene;
@FXML
private Circle circle;
//1 Frame evey 10 millis, which means 100 FPS
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
double deltaX = 2;
double deltaY = 2;
@Override
public void handle(ActionEvent actionEvent) {
circle.setLayoutX(circle.getLayoutX() + deltaX);
circle.setLayoutY(circle.getLayoutY() + deltaY);
Bounds bounds = scene.getBoundsInLocal();
boolean rightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius());
boolean leftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius());
boolean bottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius());
boolean topBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius());
if (rightBorder || leftBorder) {
deltaX *= -1;
}
if (bottomBorder || topBorder) {
deltaY *= -1;
}
}
}));
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
}
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Circle?>
<AnchorPane fx:id="scene" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Circle fx:id="circle" fill="DODGERBLUE" layoutX="100.0" layoutY="200.0" radius="25.0" stroke="BLACK" strokeType="INSIDE" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment