Skip to content

Instantly share code, notes, and snippets.

@stoffie
Created July 29, 2014 19:44
Show Gist options
  • Save stoffie/7b764277d1d1a441312f to your computer and use it in GitHub Desktop.
Save stoffie/7b764277d1d1a441312f to your computer and use it in GitHub Desktop.
package prog2;
import java.util.LinkedList;
import java.util.Random;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
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.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Paint;
import javafx.scene.shape.StrokeType;
/**
* VBox with built in style
*
* @author damiano
*/
class Sidebar extends VBox {
public Sidebar(Node... nodes) {
super(nodes);
setSpacing(10);
setPadding(new Insets(10));
setStyle("-fx-background-color: #EEEEEE;");
}
}
class CoordsRectangle extends Rectangle {
int i, j;
public CoordsRectangle(int i, int j, double d, Paint paint, double strokeSize) {
super(d, d, paint);
this.i = i;
this.j = j;
setStroke(Color.RED);
setStrokeWidth(strokeSize);
setStrokeType(StrokeType.CENTERED);
}
}
class RectGridPane extends GridPane {
static int gridSize = 8;
double strokeSize = 2.0;
double rectSize = 50.0;
CoordsRectangle mx[][] = new CoordsRectangle[gridSize][gridSize];
public RectGridPane() {
setAlignment(Pos.CENTER);
for (int i = 0; i < gridSize; ++i) {
for (int j = 0; j < gridSize; ++j) {
CoordsRectangle rect = new CoordsRectangle(i, j, rectSize, Color.WHITE, strokeSize);
addCoordsRectangle(rect);
GridPane.setHalignment(rect, HPos.CENTER);
}
}
}
CoordsRectangle removeCoordsRectangle(int i, int j) {
CoordsRectangle rect = mx[i][j];
getChildren().remove(rect);
return rect;
}
void addCoordsRectangle(CoordsRectangle r) {
mx[r.i][r.j] = r;
add(r, r.i, r.j);
}
}
class ChangeColorEventHandler implements EventHandler<MouseEvent> {
LinkedList<Color> colorsList = new LinkedList<>();
Rectangle r;
public ChangeColorEventHandler(Rectangle r) {
this.r = r;
colorsList.add(Color.GREEN);
colorsList.add(Color.BLUE);
colorsList.add(Color.MAGENTA);
colorsList.add(Color.NAVAJOWHITE);
}
@Override
public void handle(MouseEvent t) {
colorsList.addLast(colorsList.removeFirst());
r.setFill(colorsList.getFirst());
}
}
class RandomMoveEventHandler implements EventHandler<MouseEvent> {
Random rand = new Random();
RectGridPane grid;
CoordsRectangle r;
public RandomMoveEventHandler(CoordsRectangle r, RectGridPane grid) {
this.r = r;
this.grid = grid;
}
@Override
public void handle(MouseEvent t) {
int i, j, itmp, jtmp;
CoordsRectangle r2;
do {
i = rand.nextInt(RectGridPane.gridSize);
j = rand.nextInt(RectGridPane.gridSize);
} while (i == r.i && j == r.j);
r2 = grid.removeCoordsRectangle(i, j);
grid.getChildren().remove(r);
itmp = r2.i;
jtmp = r2.j;
r2.i = r.i;
r2.j = r.j;
r.i = itmp;
r.j = jtmp;
grid.addCoordsRectangle(r);
grid.addCoordsRectangle(r2);
}
}
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();
Sidebar sidebar = new Sidebar(label, radioColor, radioMove);
RectGridPane grid = new RectGridPane();
CoordsRectangle colorRect;
ChangeColorEventHandler changeColor;
RandomMoveEventHandler randomMove;
@Override
public void start(Stage primaryStage) {
colorRect = grid.mx[0][0];
radioColor.setToggleGroup(toogleGroup);
radioColor.setSelected(true);
radioColor.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
colorRect.setOnMouseClicked(changeColor);
}
});
radioMove.setToggleGroup(toogleGroup);
radioMove.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
colorRect.setOnMouseClicked(randomMove);
}
});
changeColor = new ChangeColorEventHandler(colorRect);
colorRect.setFill(changeColor.colorsList.getFirst());
randomMove = new RandomMoveEventHandler(colorRect, grid);
colorRect.setOnMouseClicked(changeColor);
root.setLeft(sidebar);
root.setCenter(grid);
primaryStage.setScene(new Scene(root, 600, 470));
primaryStage.setMinHeight(470);
primaryStage.setMinWidth(600);
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