Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created August 18, 2021 09:04
Show Gist options
  • Save Da9el00/f9f483f6b6531435a2290b386cda6691 to your computer and use it in GitHub Desktop.
Save Da9el00/f9f483f6b6531435a2290b386cda6691 to your computer and use it in GitHub Desktop.
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.Node;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private AnchorPane scene;
@FXML
private Circle circle;
//Random random = new Random();
private ArrayList<Rectangle> bricks = new ArrayList<>();
double deltaX = 1;
double deltaY = 3;
//1 Frame evey 10 millis, which means 100 FPS
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
circle.setLayoutX(circle.getLayoutX() + deltaX);
circle.setLayoutY(circle.getLayoutY() + deltaY);
checkCollisionScene(scene);
if(!bricks.isEmpty()){
bricks.removeIf(brick -> checkCollisionBricks(brick));
} else {
timeline.stop();
}
}
}));
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
timeline.setCycleCount(Animation.INDEFINITE);
createBricks();
timeline.play();
}
public void checkCollisionScene(Node node){
Bounds bounds = node.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;
}
}
public boolean checkCollisionBricks(Rectangle brick){
if(circle.getBoundsInParent().intersects(brick.getBoundsInParent())){
boolean rightBorder = circle.getLayoutX() >= ((brick.getX() + brick.getWidth()) - circle.getRadius());
boolean leftBorder = circle.getLayoutX() <= (brick.getX() + circle.getRadius());
boolean bottomBorder = circle.getLayoutY() >= ((brick.getY() + brick.getHeight()) - circle.getRadius());
boolean topBorder = circle.getLayoutY() <= (brick.getY() + circle.getRadius());
if (rightBorder || leftBorder) {
deltaX *= -1;
}
if (bottomBorder || topBorder) {
deltaY *= -1;
}
scene.getChildren().remove(brick);
return true;
}
return false;
}
public void createBricks(){
double width = 560;
double height = 200;
int spaceCheck = 1;
for (double i = height; i > 0 ; i = i - 50) {
for (double j = width; j > 0 ; j = j - 25) {
if(spaceCheck % 2 == 0){
Rectangle rectangle = new Rectangle(j,i,30,30);
rectangle.setFill(Color.RED);
scene.getChildren().add(rectangle);
bricks.add(rectangle);
}
spaceCheck++;
}
}
}
}
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="300.0" layoutY="300.0" radius="5.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