Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.animation.TranslateTransition;
  4. import javafx.application.Application;
  5. import javafx.event.ActionEvent;
  6. import javafx.scene.Scene;
  7. import javafx.scene.layout.StackPane;
  8. import javafx.scene.paint.Color;
  9. import javafx.scene.shape.Rectangle;
  10. import javafx.stage.Stage;
  11. import javafx.util.Duration;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. public class Main extends Application {
  17.  
  18.  
  19.     public Rectangle formBox() {
  20.         Rectangle rectangle = new Rectangle(0, 90, 500, 500);
  21.         rectangle.setFill(null);
  22.         rectangle.setStroke(Color.BLACK);
  23.         return rectangle;
  24.     }
  25.  
  26.     public void animate(StackPane root) {
  27.         List<Particle> particles = new ArrayList<>();
  28.         Particle p1 = new Particle(new Vector(0, 0), new Vector(2, 1), 1, 1);
  29.         Particle p2 = new Particle(new Vector(29, 60), new Vector(1, 1), 1, 1);
  30.         particles.add(p1);
  31.         particles.add(p2);
  32.         Simulation simulation = new Simulation(particles, 500, 1000);
  33.         for (int i = 0; i < 5; i++) {
  34.             simulation.step();
  35.         }
  36.         Rectangle rectangle = formBox();
  37.         root.getChildren().add(rectangle);
  38.         List<TranslateTransition> ttList = new ArrayList<>();
  39.         for (Particle initialParticle : simulation.getTrajectory().get(0)) {
  40.             root.getChildren().add(initialParticle.getCircle());
  41.             ttList.add(new TranslateTransition(Duration.seconds(simulation.getDt()/100), initialParticle.getCircle()));
  42.         }
  43.         System.out.println(ttList.size());
  44.         for (List<Particle> state : simulation.getTrajectory()) {
  45.             int currentStateIndex = simulation.getTrajectory().indexOf(state);
  46.             if (currentStateIndex == simulation.getTrajectory().size() - 1) {
  47.                 return;
  48.             } else {
  49.                 for (Particle particle : state) {
  50.                     int currentParticleIndex = simulation.getTrajectory().get(currentStateIndex).indexOf(particle);
  51.                     Particle futureParticle = simulation.getTrajectory().get(currentStateIndex + 1).get(currentParticleIndex);
  52.  
  53.                     ttList.get(currentParticleIndex).setFromX(particle.getPosition().getX());
  54.                     ttList.get(currentParticleIndex).setFromY(particle.getPosition().getY());
  55.                     ttList.get(currentParticleIndex).setToX(futureParticle.getPosition().getX());
  56.                     ttList.get(currentParticleIndex).setToY(futureParticle.getPosition().getY());
  57.                     ttList.get(currentParticleIndex).setCycleCount(1);
  58.                     System.out.println("Location before relocation = " + particle.getCircle().getCenterX() + ", " + particle.getCircle().getCenterY());
  59.                     ttList.get(currentParticleIndex).setOnFinished((ActionEvent event) -> {
  60.                         System.out.println("Location after relocation = " + particle.getCircle().getTranslateX() + ", " + particle.getCircle().getTranslateY());
  61.                     });
  62.                     ttList.get(currentParticleIndex).play();
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     public void start(Stage primaryStage) throws Exception {
  70.         StackPane root = new StackPane();
  71.         animate(root);
  72.         primaryStage.setTitle("Particle Collision Simulation");
  73.         primaryStage.setScene(new Scene(root, 550, 550));
  74.         primaryStage.show();
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         launch(args);
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement