import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.layout.GridPane; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.shape.Rectangle; import javafx.scene.control.ScrollPane; import javafx.scene.paint.Color; import java.util.Random; import java.util.ArrayList; /* Nyttig til oblig 7? To knapper - en legger til ruter. Den andre gir en tilfeldig rute fargen bl?. */ public class Ruter extends Application{ ArrayList firkanter = new ArrayList(); Random rand = new Random(); int radteller = 1; public void start(Stage stage){ GridPane grid = new GridPane(); ScrollPane skroll = new ScrollPane(grid); Scene scene = new Scene(skroll); stage.setScene(scene); Button leggTilRadKnapp = new Button("Ny rad"); Button fargeKnapp = new Button("Ny farge"); grid.setVgap(20); grid.setHgap(20); grid.add(leggTilRadKnapp, 0,0); grid.add(fargeKnapp, 1,0); leggTilRadKnapp.setOnAction(e ->{ Rectangle a = new Rectangle(50, 50); Rectangle b = new Rectangle(50, 50); Rectangle c = new Rectangle(50, 50); firkanter.add(a); firkanter.add(b); firkanter.add(c); grid.addRow(radteller++,a,b,c); //stage.sizeToScene(); }); fargeKnapp.setOnAction(e->{ if(firkanter.size() == 0) return; int i = rand.nextInt(firkanter.size()); while(firkanter.get(i).getFill().equals(Color.BLUE)){ i = rand.nextInt(firkanter.size()); } firkanter.get(i).setFill(Color.BLUE); }); stage.setMaxHeight(800); stage.setMinWidth(300); stage.setMinHeight(300); stage.show(); } }