Created
July 29, 2014 15:13
-
-
Save stoffie/1b2b4e7a60ab3fe6da4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package prog2.colorgrid; | |
import javafx.beans.binding.Bindings; | |
import javafx.beans.binding.NumberBinding; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Rectangle; | |
import javafx.application.Application; | |
import javafx.geometry.HPos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.RadioButton; | |
import javafx.stage.Stage; | |
import javafx.scene.control.ToggleGroup; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.VBox; | |
public class ColorGrid extends Application { | |
BorderPane root = new BorderPane(); | |
Label label = new Label("Select mode:"); | |
RadioButton radioColor = new RadioButton("Change color"); | |
RadioButton radioMove = new RadioButton("Random movevent"); | |
ToggleGroup toogleGroup = new ToggleGroup(); | |
VBox sidebar = new VBox(label, radioColor, radioMove); | |
@Override | |
public void start(Stage primaryStage) { | |
radioColor.setToggleGroup(toogleGroup); | |
radioMove.setToggleGroup(toogleGroup); | |
int size = 8; | |
double gapSize = 1.0; | |
GridPane grid = new GridPane(); | |
grid.setHgap(gapSize); | |
grid.setVgap(gapSize); | |
grid.setGridLinesVisible(true); | |
NumberBinding rectsAreaSize = Bindings.min(grid.heightProperty(), grid.widthProperty()); | |
for (int i = 0; i < size; ++i) { | |
for (int j = 0; j < size; ++j) { | |
Rectangle rect = new Rectangle(5.0, 5.0, Color.WHITE); | |
rect.widthProperty().bind(rectsAreaSize.subtract(gapSize * size).divide(size)); | |
rect.heightProperty().bind(rectsAreaSize.subtract(gapSize * size).divide(size)); | |
grid.add(rect, i, j); | |
GridPane.setHalignment(rect, HPos.CENTER); | |
} | |
} | |
root.setLeft(sidebar); | |
root.setCenter(grid); | |
primaryStage.setScene(new Scene(root, 400, 400)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment