Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.animation.TranslateTransition;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- 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;
- import java.util.ArrayList;
- import java.util.List;
- public class Main extends Application {
- public Rectangle formBox() {
- Rectangle rectangle = new Rectangle(0, 90, 500, 500);
- rectangle.setFill(null);
- rectangle.setStroke(Color.BLACK);
- return rectangle;
- }
- public void animate(StackPane root) {
- List<Particle> particles = new ArrayList<>();
- Particle p1 = new Particle(new Vector(0, 0), new Vector(2, 1), 1, 1);
- Particle p2 = new Particle(new Vector(29, 60), new Vector(1, 1), 1, 1);
- particles.add(p1);
- particles.add(p2);
- Simulation simulation = new Simulation(particles, 500, 1000);
- for (int i = 0; i < 5; i++) {
- simulation.step();
- }
- Rectangle rectangle = formBox();
- root.getChildren().add(rectangle);
- List<TranslateTransition> ttList = new ArrayList<>();
- for (Particle initialParticle : simulation.getTrajectory().get(0)) {
- root.getChildren().add(initialParticle.getCircle());
- ttList.add(new TranslateTransition(Duration.seconds(simulation.getDt()/100), initialParticle.getCircle()));
- }
- System.out.println(ttList.size());
- for (List<Particle> state : simulation.getTrajectory()) {
- int currentStateIndex = simulation.getTrajectory().indexOf(state);
- if (currentStateIndex == simulation.getTrajectory().size() - 1) {
- return;
- } else {
- for (Particle particle : state) {
- int currentParticleIndex = simulation.getTrajectory().get(currentStateIndex).indexOf(particle);
- Particle futureParticle = simulation.getTrajectory().get(currentStateIndex + 1).get(currentParticleIndex);
- ttList.get(currentParticleIndex).setFromX(particle.getPosition().getX());
- ttList.get(currentParticleIndex).setFromY(particle.getPosition().getY());
- ttList.get(currentParticleIndex).setToX(futureParticle.getPosition().getX());
- ttList.get(currentParticleIndex).setToY(futureParticle.getPosition().getY());
- ttList.get(currentParticleIndex).setCycleCount(1);
- System.out.println("Location before relocation = " + particle.getCircle().getCenterX() + ", " + particle.getCircle().getCenterY());
- ttList.get(currentParticleIndex).setOnFinished((ActionEvent event) -> {
- System.out.println("Location after relocation = " + particle.getCircle().getTranslateX() + ", " + particle.getCircle().getTranslateY());
- });
- ttList.get(currentParticleIndex).play();
- }
- }
- }
- }
- @Override
- public void start(Stage primaryStage) throws Exception {
- StackPane root = new StackPane();
- animate(root);
- primaryStage.setTitle("Particle Collision Simulation");
- primaryStage.setScene(new Scene(root, 550, 550));
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement