Advertisement
Adam9383

Animace

Jan 22nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. public class FXMLDocumentController implements Initializable {
  2.  
  3.     @FXML
  4.     private Slider spinnerTrvání;
  5.     @FXML
  6.     private CheckBox cbAutoReverse;
  7.     @FXML
  8.     private CheckBox cbPrizpusobeniSmeru;
  9.     @FXML
  10.     private CheckBox cbOtoceniNaKonci;
  11.     @FXML
  12.     private Pane pane;
  13.    
  14.     private ObservableList<Node> tvary;
  15.     private ObservableList<Circle> body;
  16.     private Path path;
  17.     private PathTransition pt;
  18.     @FXML
  19.     private Button stopBtn;
  20.     private final Image im = new Image("obrazky/lokomotiva.png");
  21.     private ImageView iv;
  22.    
  23.     @Override
  24.     public void initialize(URL url, ResourceBundle rb) {
  25.         cbAutoReverse.setSelected(true);
  26.         cbPrizpusobeniSmeru.setSelected(true);
  27.         tvary = pane.getChildren();
  28.         body = FXCollections.observableArrayList();
  29.         stopBtn.setDisable(true);
  30.         iv = new ImageView(im);
  31.     }    
  32.  
  33.     @FXML
  34.     private void vymazVseButton(ActionEvent event) {
  35.     }
  36.  
  37.     @FXML
  38.     private void startButton(ActionEvent event) {
  39.         if (body.size() > 1) {
  40.             if (iv.getImage() != null) {
  41.                 tvary.remove(iv);
  42.             }
  43.             pt = new PathTransition(Duration.seconds(spinnerTrvání.getValue()), path);
  44.             iv.setFitHeight(60);
  45.             iv.setFitWidth(60);
  46.             pt.setNode(iv);
  47.  
  48.             if (cbAutoReverse.isSelected()) {
  49.                 pt.setAutoReverse(true);
  50.             } else {
  51.                 pt.setAutoReverse(false);
  52.             }
  53.  
  54.             if (cbPrizpusobeniSmeru.isSelected()) {
  55.                 pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
  56.             } else {
  57.                 pt.setOrientation(PathTransition.OrientationType.NONE);
  58.             }
  59.  
  60.             if (cbOtoceniNaKonci.isSelected()) {
  61.                 pt.setCycleCount(2);
  62.             } else {
  63.                 pt.setCycleCount(1);
  64.             }
  65.  
  66.             tvary.add(iv);
  67.             pt.play();
  68.             stopBtn.setDisable(false);
  69.            
  70.         }    
  71.        
  72.     }
  73.  
  74.     @FXML
  75.     private void stopButton(ActionEvent event) {
  76.         pt.stop();
  77.         tvary.remove(iv);
  78.         stopBtn.setDisable(true);
  79.     }
  80.  
  81.     @FXML
  82.     private void mouseClicked(MouseEvent event) {
  83.         if (event.getButton() == MouseButton.PRIMARY) {
  84.             Circle c = new Circle(event.getX(), event.getY(), 5);
  85.             c.setStroke(Color.BLACK);
  86.             c.setFill(Color.AQUAMARINE);
  87.            
  88.             c.setOnMouseClicked(value -> {
  89.                 if (value.getButton() == MouseButton.SECONDARY) {
  90.                     tvary.clear();
  91.                     body.remove(c);
  92.                     if (body.size() > 1) {
  93.                         udelejDrahu();
  94.                     }
  95.                     tvary.addAll(body);
  96.                 }
  97.             });
  98.  
  99.             tvary.add(c);
  100.             body.add(c);
  101.            
  102.             if (body.size() > 1) {
  103.                 udelejDrahu();
  104.             }
  105.            
  106.         }    
  107.     }
  108.  
  109.     private void udelejDrahu() {
  110.         path = new Path();
  111.        
  112.         MoveTo mt = new MoveTo(body.get(0).getCenterX(),body.get(0).getCenterY());
  113.         path.getElements().add(mt);
  114.        
  115.         for (int i = 1; i < body.size(); i++) {
  116.             LineTo lt = new LineTo(body.get(i).getCenterX(),body.get(i).getCenterY());
  117.             path.getElements().add(lt);
  118.             Line line = new Line(body.get(i-1).getCenterX(),body.get(i-1).getCenterY(),
  119.                     body.get(i).getCenterX(),body.get(i).getCenterY());
  120.             line.setStroke(Color.BLUE);
  121.             line.getStrokeDashArray().addAll(10d, 10d);
  122.             tvary.add(line);
  123.         }
  124.     }
  125.    
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement