Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created April 17, 2021 09:16
Show Gist options
  • Save Da9el00/73adff8130cbabf49bc12f0c168c4134 to your computer and use it in GitHub Desktop.
Save Da9el00/73adff8130cbabf49bc12f0c168c4134 to your computer and use it in GitHub Desktop.
Moving rectangles
package sample;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
public class Controller implements Initializable{
@FXML
private AnchorPane anchorPane;
TranslateTransition transition;
TranslateTransition transition_1;
Random rand = new Random();
@FXML
void start(ActionEvent event) {
resetRectangles();
transition.play();
transition_1.play();
}
public void resetRectangles(){
int recHeightX = rand.nextInt(250);
int recHeightX_1 = rand.nextInt(250);
int recHeight = 25 + rand.nextInt(50);
int recWidth = 25 + rand.nextInt(50);
int recHeight_1 = 25 + rand.nextInt(50);
int recWidth_1 = 25 + rand.nextInt(50);
Rectangle rectangle = new Rectangle(-100,recHeightX,recHeight,recWidth);
Rectangle rectangle_1 = new Rectangle(600,recHeightX_1,recHeight_1,recWidth_1);
transition = new TranslateTransition();
transition_1 = new TranslateTransition();
anchorPane.getChildren().addAll(rectangle,rectangle_1);
rectangle.setFill(Color.web("#2191FB"));
rectangle_1.setFill(Color.web("#BA274A"));
//Rectangle transition
transition.setNode(rectangle);
transition.setDuration(Duration.seconds(5));
transition.setToX(700);
//Rectangle_1 transition
transition_1.setNode(rectangle_1);
transition_1.setDuration(Duration.seconds(5));
transition_1.setToX(-700);
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.1), e->{
resetRectangles();
transition.play();
transition_1.play();
}));
timeline.setCycleCount(Timeline.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.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="anchorPane" 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>
<Button layoutX="280.0" layoutY="347.0" mnemonicParsing="false" onAction="#start" text="Start" />
</children>
</AnchorPane>
@TimothyMunene
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment