Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package circles;
- import java.util.stream.Stream;
- import javafx.animation.Animation;
- import javafx.animation.ScaleTransition;
- import javafx.animation.TranslateTransition;
- import javafx.application.Application;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.Spinner;
- import javafx.scene.control.Slider;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- /**
- * A lab exercise to introduce Java 8 lambdas and streams.
- * @author Your name here
- */
- public class Circles extends Application {
- public static final int ROWS = 4;
- public static final int COLS = 5;
- public static final int CELL_SIZE = 100;
- @Override
- public void start(Stage primaryStage) {
- root = new VBox();
- holder = new HBox();
- canvas = new Pane();
- rowSpinner = new <Integer>Spinner(1,5,5);
- colSpinner = new <Integer>Spinner(1,5,5);
- xScale = new <Integer>Spinner(-3,3,0);
- yScale = new <Integer>Spinner(-3,3,0);
- sizeSlider = new Slider(50,150,100);
- root.setAlignment(Pos.CENTER);
- canvas.setPrefSize(colSpinner.getValue() * sizeSlider.getValue(), rowSpinner.getValue() * sizeSlider.getValue());
- // addButtonHandler(); // You must write
- holder.getChildren().addAll(new Label("Rows"), rowSpinner, new Label("Columns"),colSpinner, new Label("Cell Size"),sizeSlider,
- new Label("X Scale"), xScale, new Label("Y Scale"), yScale);
- holder.setSpacing(20);
- holder.setAlignment(Pos.BOTTOM_CENTER);
- root.getChildren().addAll(canvas, holder);
- primaryStage.setTitle("Java 8 Lab Exercise");
- primaryStage.setScene(new Scene(root));
- primaryStage.show();
- //makeRow().forEach(x -> System.out.println(x));
- makeAllRows().forEach(r -> r.forEach(x -> System.out.println(x)));
- rowSpinner.valueProperty().addListener(e -> { canvas.getChildren().clear();
- addAllRowsToCanvas(makeAllRows()); });
- colSpinner.valueProperty().addListener(e -> { canvas.getChildren().clear();
- addAllRowsToCanvas(makeAllRows()); });
- xScale.valueProperty().addListener(e -> { canvas.getChildren().clear();
- addAllRowsToCanvas(makeAllRows()); });
- yScale.valueProperty().addListener(e -> { canvas.getChildren().clear();
- addAllRowsToCanvas(makeAllRows()); });
- sizeSlider.valueProperty().addListener(e -> { canvas.getChildren().clear();
- addAllRowsToCanvas(makeAllRows()); });
- addAllRowsToCanvas(makeAllRows());
- }
- /**
- * This method adds the handler to the button that gives
- * this application its behavior.
- */
- // private void addButtonHandler() {
- // starter.setOnAction(e -> { canvas.getChildren().clear();
- // addAllRowsToCanvas(makeAllRows()); });
- // }
- private HBox holder;
- private VBox root;
- private Pane canvas;
- private Spinner<Integer> rowSpinner, colSpinner, xScale, yScale;
- private Slider sizeSlider;
- /**
- * @param args the command line arguments
- */
- private void addToCanvas(Circle circle){
- circle.setFill(new Color(Math.random(),Math.random(),Math.random(),1));
- double fromX = 50+colSpinner.getValue()*sizeSlider.getValue();
- double fromY = 50+rowSpinner.getValue()*sizeSlider.getValue();
- double toX = 50 + cols * sizeSlider.getValue();
- double toY = 50 + rows * sizeSlider.getValue();
- circle.setCenterX(fromX);
- circle.setCenterY(fromY);
- canvas.getChildren().add(circle);
- TranslateTransition tt = new TranslateTransition(Duration.millis(500));
- tt.setNode(circle);
- tt.setByX(toX-fromX);
- tt.setByY(toY-fromY);
- tt.play();
- ScaleTransition st = new ScaleTransition(Duration.millis(Math.random() * (2000-500) + 500));
- st.setNode(circle);
- st.setByX(xScale.getValue());
- st.setByY(yScale.getValue());
- st.setCycleCount(Animation.INDEFINITE);
- st.setAutoReverse(true);
- st.play();
- }
- private Stream<Circle> makeRow(){
- Stream<Circle> stream = Stream.generate(() -> new Circle(sizeSlider.getValue()/2)).limit(colSpinner.getValue());
- return stream;
- }
- private void addRowToCanvas(Stream<Circle> stream){
- cols = 0;
- stream.forEach(c -> {addToCanvas(c); cols++;});
- }
- private Stream<Stream<Circle>> makeAllRows(){
- Stream<Stream<Circle>> circles = Stream.generate(() -> makeRow()).limit(rowSpinner.getValue());
- return circles;
- }
- private void addAllRowsToCanvas(Stream<Stream<Circle>> circles){
- rows = 0;
- circles.forEach(r -> {addRowToCanvas(r); rows++;});
- }
- public static void main(String[] args) {
- launch(args);
- }
- private int rows = 1;
- private int cols = 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement