Last active
March 17, 2016 14:21
-
-
Save fado/14112207351306a35e9f 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 org.fadonet.blueprint; | |
import javafx.application.*; | |
import javafx.scene.*; | |
import javafx.scene.control.*; | |
import javafx.scene.input.*; | |
import javafx.scene.layout.*; | |
import javafx.scene.paint.*; | |
import javafx.scene.shape.*; | |
import javafx.stage.*; | |
public class Blueprint extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Grid grid = new Grid(); | |
BlueprintMenuBar menuBar = new BlueprintMenuBar(grid); | |
MainUI mainUI = new MainUI(menuBar, grid); | |
Scene main = new Scene(mainUI); | |
primaryStage.setScene(main); | |
primaryStage.show(); | |
primaryStage.setOnCloseRequest(event -> System.exit(0)); | |
} | |
} | |
class MainUI extends GridPane { | |
public MainUI(BlueprintMenuBar menuBar, Grid grid) { | |
GridPane.setConstraints(menuBar, 0, 0); | |
GridPane.setConstraints(grid, 0, 1); | |
this.getChildren().addAll(menuBar, grid); | |
} | |
} | |
class BlueprintMenuBar extends MenuBar { | |
public BlueprintMenuBar(Grid grid) { | |
Menu fileMenu = new Menu("File"); | |
MenuItem calculate = new MenuItem("Calculate"); | |
calculate.setOnAction(event -> grid.calculate()); | |
fileMenu.getItems().add(calculate); | |
getMenus().add(fileMenu); | |
setUseSystemMenuBar(true); | |
setMinWidth(500); | |
} | |
} | |
class Grid extends GridPane { | |
static final int ROWS = 17; | |
static final int COLUMNS = 106; | |
Square[][] allSquares = new Square[ROWS][COLUMNS]; | |
public Grid() { | |
for(int y=0; y<ROWS; y++) { | |
for(int x=0; x<COLUMNS; x++) { | |
Square square = new Square(x, y); | |
allSquares[y][x] = square; | |
GridPane.setConstraints(square, x, y); | |
this.getChildren().add(square); | |
} | |
} | |
} | |
public void calculate() { | |
long total = 0; | |
double currentPower = 0; | |
int multiple = 17; | |
for(int x=COLUMNS-1; x>=0; x--) { | |
for(int y=0; y<ROWS; y++) { | |
if(!allSquares[y][x].isEmpty()) { | |
total += Math.pow(2, currentPower) * multiple; | |
} | |
currentPower++; | |
} | |
} | |
System.out.println(total); | |
} | |
} | |
class Square extends StackPane { | |
static final int GRID_SQUARE_SIZE = 10; | |
public final int x; | |
public final int y; | |
private Rectangle rectangle; | |
private Boolean empty = true; | |
public Square(int x, int y) { | |
this.x = x; | |
this.y = y; | |
this.setMinWidth(GRID_SQUARE_SIZE); | |
this.setMinHeight(GRID_SQUARE_SIZE); | |
this.rectangle = getRectangle(); | |
this.getChildren().add(rectangle); | |
setOnMouseClicked(this::doMouseClicked); | |
} | |
private void doMouseClicked(MouseEvent event) { | |
if(event.getButton() == MouseButton.PRIMARY) { | |
this.setEmpty(false); | |
this.rectangle.setFill(Color.BLACK); | |
} | |
if(event.getButton() == MouseButton.SECONDARY) { | |
this.setEmpty(true); | |
this.rectangle.setFill(Color.TRANSPARENT); | |
} | |
} | |
private Rectangle getRectangle() { | |
Rectangle rectangle = new Rectangle(); | |
rectangle.setHeight(GRID_SQUARE_SIZE); | |
rectangle.setWidth(GRID_SQUARE_SIZE); | |
rectangle.setFill(Color.TRANSPARENT); | |
rectangle.setStroke(Color.BLACK); | |
rectangle.setStrokeType(StrokeType.INSIDE); | |
rectangle.setStrokeWidth(0.25); | |
return rectangle; | |
} | |
private void setEmpty(Boolean empty) { | |
this.empty = empty; | |
} | |
public Boolean isEmpty() { | |
return this.empty; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment