Advertisement
elltyl325

circles

Oct 26th, 2020
3,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. package circles;
  2.  
  3. import java.util.stream.Stream;
  4. import javafx.animation.Animation;
  5. import javafx.animation.ScaleTransition;
  6. import javafx.animation.TranslateTransition;
  7. import javafx.application.Application;
  8. import javafx.geometry.Pos;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.Label;
  12. import javafx.scene.control.Spinner;
  13. import javafx.scene.control.Slider;
  14. import javafx.scene.layout.Pane;
  15. import javafx.scene.layout.HBox;
  16. import javafx.scene.layout.VBox;
  17. import javafx.scene.paint.Color;
  18. import javafx.scene.shape.Circle;
  19. import javafx.stage.Stage;
  20. import javafx.util.Duration;
  21.  
  22. /**
  23.  * A lab exercise to introduce Java 8 lambdas and streams.
  24.  * @author Your name here
  25.  */
  26. public class Circles extends Application {
  27.    
  28.     public static final int ROWS = 4;
  29.     public static final int COLS = 5;
  30.     public static final int CELL_SIZE = 100;
  31.    
  32.     @Override
  33.     public void start(Stage primaryStage) {
  34.         root = new VBox();
  35.         holder = new HBox();
  36.         canvas = new Pane();
  37.         rowSpinner = new <Integer>Spinner(1,5,5);
  38.         colSpinner = new <Integer>Spinner(1,5,5);
  39.         xScale = new <Integer>Spinner(-3,3,0);
  40.         yScale = new <Integer>Spinner(-3,3,0);
  41.         sizeSlider = new Slider(50,150,100);
  42.        
  43.        
  44.         root.setAlignment(Pos.CENTER);
  45.         canvas.setPrefSize(colSpinner.getValue() * sizeSlider.getValue(), rowSpinner.getValue() * sizeSlider.getValue());
  46.        
  47. //        addButtonHandler();  // You must write
  48.  
  49.         holder.getChildren().addAll(new Label("Rows"), rowSpinner, new Label("Columns"),colSpinner, new Label("Cell Size"),sizeSlider,
  50.                                           new Label("X Scale"), xScale, new Label("Y Scale"), yScale);
  51.  
  52.         holder.setSpacing(20);
  53.         holder.setAlignment(Pos.BOTTOM_CENTER);
  54.         root.getChildren().addAll(canvas, holder);
  55.         primaryStage.setTitle("Java 8 Lab Exercise");
  56.         primaryStage.setScene(new Scene(root));
  57.         primaryStage.show();
  58.         //makeRow().forEach(x -> System.out.println(x));
  59.         makeAllRows().forEach(r -> r.forEach(x -> System.out.println(x)));
  60.        
  61.         rowSpinner.valueProperty().addListener(e -> { canvas.getChildren().clear();
  62.                                                     addAllRowsToCanvas(makeAllRows()); });
  63.         colSpinner.valueProperty().addListener(e -> { canvas.getChildren().clear();
  64.                                                     addAllRowsToCanvas(makeAllRows()); });
  65.         xScale.valueProperty().addListener(e -> { canvas.getChildren().clear();
  66.                                                     addAllRowsToCanvas(makeAllRows()); });
  67.         yScale.valueProperty().addListener(e -> { canvas.getChildren().clear();
  68.                                                     addAllRowsToCanvas(makeAllRows()); });
  69.         sizeSlider.valueProperty().addListener(e -> { canvas.getChildren().clear();
  70.                                                     addAllRowsToCanvas(makeAllRows()); });
  71.         addAllRowsToCanvas(makeAllRows());
  72.     }
  73.    
  74.     /**
  75.      * This method adds the handler to the button that gives
  76.      * this application its behavior.
  77.      */
  78. //    private void addButtonHandler() {
  79. //        starter.setOnAction(e -> { canvas.getChildren().clear();
  80. //                                    addAllRowsToCanvas(makeAllRows()); });
  81. //    }
  82.    
  83.     private HBox holder;
  84.     private VBox root;
  85.     private Pane canvas;
  86.     private Spinner<Integer> rowSpinner, colSpinner, xScale, yScale;
  87.     private Slider sizeSlider;
  88.  
  89.     /**
  90.      * @param args the command line arguments
  91.      */
  92.    
  93.     private void addToCanvas(Circle circle){
  94.         circle.setFill(new Color(Math.random(),Math.random(),Math.random(),1));
  95.         double fromX = 50+colSpinner.getValue()*sizeSlider.getValue();
  96.         double fromY = 50+rowSpinner.getValue()*sizeSlider.getValue();
  97.         double toX = 50 + cols * sizeSlider.getValue();
  98.         double toY = 50 + rows * sizeSlider.getValue();
  99.         circle.setCenterX(fromX);
  100.         circle.setCenterY(fromY);
  101.         canvas.getChildren().add(circle);
  102.         TranslateTransition tt = new TranslateTransition(Duration.millis(500));
  103.         tt.setNode(circle);
  104.         tt.setByX(toX-fromX);
  105.         tt.setByY(toY-fromY);
  106.         tt.play();
  107.         ScaleTransition st = new ScaleTransition(Duration.millis(Math.random() * (2000-500) + 500));
  108.         st.setNode(circle);
  109.         st.setByX(xScale.getValue());
  110.         st.setByY(yScale.getValue());
  111.         st.setCycleCount(Animation.INDEFINITE);
  112.         st.setAutoReverse(true);
  113.         st.play();
  114.     }
  115.    
  116.     private Stream<Circle> makeRow(){
  117.       Stream<Circle> stream = Stream.generate(() -> new Circle(sizeSlider.getValue()/2)).limit(colSpinner.getValue());  
  118.       return stream;
  119.     }
  120.    
  121.     private void addRowToCanvas(Stream<Circle> stream){
  122.         cols = 0;
  123.         stream.forEach(c -> {addToCanvas(c); cols++;});
  124.     }
  125.    
  126.     private Stream<Stream<Circle>> makeAllRows(){
  127.         Stream<Stream<Circle>> circles = Stream.generate(() -> makeRow()).limit(rowSpinner.getValue());
  128.         return circles;
  129.     }
  130.    
  131.     private void addAllRowsToCanvas(Stream<Stream<Circle>> circles){
  132.         rows = 0;
  133.         circles.forEach(r -> {addRowToCanvas(r); rows++;});
  134.        
  135.     }
  136.     public static void main(String[] args) {
  137.         launch(args);
  138.     }
  139.    
  140.     private int rows = 1;
  141.     private int cols = 1;
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement