Created
May 8, 2019 09:37
-
-
Save rikmeijer/ba88cb9f5e583f1a2dee377bdc0f3175 to your computer and use it in GitHub Desktop.
PROG4_Wk3b
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 PROG4_WK3b; | |
import javafx.application.Application; | |
import javafx.event.EventHandler; | |
import javafx.stage.Stage; | |
import javafx.scene.Scene; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.GridPane; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
try { | |
Squares scene = new Squares(400, 300); | |
scene.enterStage(primaryStage); | |
primaryStage.show(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
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 PROG4_WK3b; | |
import javafx.beans.value.ChangeListener; | |
import javafx.beans.value.ObservableValue; | |
import javafx.event.Event; | |
import javafx.event.EventHandler; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.scene.input.MouseEvent; | |
import javafx.scene.layout.Background; | |
import javafx.scene.layout.BackgroundFill; | |
import javafx.scene.layout.Border; | |
import javafx.scene.layout.BorderStroke; | |
import javafx.scene.layout.BorderStrokeStyle; | |
import javafx.scene.layout.BorderWidths; | |
import javafx.scene.layout.ColumnConstraints; | |
import javafx.scene.layout.GridPane; | |
import javafx.scene.layout.RowConstraints; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.paint.Paint; | |
import javafx.stage.Stage; | |
public class Squares { | |
final private int cols = 3; | |
final private int rows = 2; | |
private Scene scene; | |
public Squares(int width, int height) { | |
super(); | |
GridPane gridPane = new GridPane(); | |
// create the square panes | |
for (int c = 0; c < cols; c++) { | |
for (int r = 0; r < rows; r++) { | |
Color mycolor = Color.rgb(r * 50 + 50, c * 30 + 30, r + c * 60 + 60); | |
StackPane sqp = new StackPane(); | |
sqp.setBackground(new Background(new BackgroundFill(mycolor, null, null))); | |
// sqp.focusedProperty().addListener(new ChangeListener<Boolean>() { | |
// @Override | |
// public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, | |
// Boolean newValue) { | |
// if (newValue) { | |
// sqp.setBorder(new Border(new BorderStroke(Color.WHITE, BorderStrokeStyle.SOLID, null, new BorderWidths(3)))); | |
// } else { | |
// sqp.setBorder(null); | |
// } | |
// | |
// } | |
// }); | |
sqp.focusedProperty().addListener((observable, oldValue, newValue) -> { | |
if (newValue) { | |
sqp.setBorder(new Border(new BorderStroke(Color.WHITE, BorderStrokeStyle.SOLID, null, new BorderWidths(3)))); | |
} else { | |
sqp.setBorder(null); | |
} | |
}); | |
// sqp.setOnMouseEntered(new EventHandler<MouseEvent>() { | |
// | |
// @Override | |
// public void handle(MouseEvent event) { | |
// sqp.requestFocus(); | |
// } | |
// }); | |
sqp.setOnMouseEntered(evt -> sqp.requestFocus()); | |
gridPane.add(sqp, c, r); | |
} | |
} | |
ColumnConstraints columnConstraint = new ColumnConstraints(); | |
columnConstraint.setPercentWidth(100d / 3d); | |
RowConstraints rowConstraint = new RowConstraints(); | |
rowConstraint.setPercentHeight(50); | |
gridPane.getRowConstraints().addAll(rowConstraint, rowConstraint); | |
gridPane.getColumnConstraints().addAll(columnConstraint, columnConstraint, columnConstraint); | |
scene = new Scene(gridPane, width, height); | |
} | |
public void enterStage(Stage stage) { | |
stage.setScene(scene); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment