Merge branch 'master' of github.com:Selebrator/omp-ha-2019
commit
66905d4f0e
@ -0,0 +1,50 @@
|
||||
package _9._1;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Slider;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.paint.Paint;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class ColorMixer extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) {
|
||||
VBox vBox = new VBox(10);
|
||||
vBox.setAlignment(Pos.TOP_CENTER);
|
||||
vBox.setPadding(new Insets(10));
|
||||
|
||||
Rectangle rectangle = new Rectangle(200, 200);
|
||||
|
||||
Slider red = colorSlider(Color.RED);
|
||||
Slider green = colorSlider(Color.GREEN);
|
||||
Slider blue = colorSlider(Color.BLUE);
|
||||
red.valueProperty().addListener((observable, oldValue, newValue) -> rectangle.setFill(Color.rgb(newValue.intValue(), (int) green.getValue(), (int) blue.getValue())));
|
||||
green.valueProperty().addListener((observable, oldValue, newValue) -> rectangle.setFill(Color.rgb((int) red.getValue(), newValue.intValue(), (int) blue.getValue())));
|
||||
blue.valueProperty().addListener((observable, oldValue, newValue) -> rectangle.setFill(Color.rgb((int) red.getValue(), (int) green.getValue(), newValue.intValue())));
|
||||
|
||||
vBox.getChildren().addAll(rectangle, red, green, blue);
|
||||
Scene scene = new Scene(new StackPane(vBox), 600, 360);
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("Color-Mixer");
|
||||
stage.setResizable(false);
|
||||
stage.show();
|
||||
}
|
||||
|
||||
private Slider colorSlider(Paint fill) {
|
||||
Slider slider = new Slider(0, 255, 0);
|
||||
slider.setShowTickLabels(true);
|
||||
slider.setShowTickMarks(true);
|
||||
slider.setBackground(new Background(new BackgroundFill(fill, CornerRadii.EMPTY, Insets.EMPTY)));
|
||||
return slider;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
package _9._2;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.paint.Paint;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Lights extends Application {
|
||||
|
||||
private static final int DEFAULT_SIZE = 4;
|
||||
private static final int GAP = 2;
|
||||
private static final Paint ON = Color.YELLOW;
|
||||
private static final Paint OFF = Color.WHITE;
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
final int size = Optional.ofNullable(this.getParameters().getNamed().get("size"))
|
||||
.map(Integer::parseInt).orElse(DEFAULT_SIZE);
|
||||
GridPane grid = new GridPane();
|
||||
Scene scene = new Scene(grid, 600, 600);
|
||||
scene.setFill(Color.BLACK);
|
||||
grid.setHgap(GAP);
|
||||
grid.setVgap(GAP);
|
||||
for(int x = 0; x < size; x++) {
|
||||
for(int y = 0; y < size; y++) {
|
||||
Rectangle rec = new Rectangle(150, 150);
|
||||
rec.heightProperty().bind(scene.heightProperty().subtract(size * GAP).divide(size));
|
||||
rec.widthProperty().bind(scene.widthProperty().subtract(size * GAP).divide(size));
|
||||
rec.setFill(OFF);
|
||||
grid.add(rec, x, y);
|
||||
}
|
||||
}
|
||||
for(int x = 0; x < size; x++) {
|
||||
for(int y = 0; y < size; y++) {
|
||||
Rectangle rec = getNodeByRowColumnIndex(x, y, grid);
|
||||
assert rec != null;
|
||||
Rectangle up = getNodeByRowColumnIndex(x, y - 1, grid);
|
||||
Rectangle down = getNodeByRowColumnIndex(x, y + 1, grid);
|
||||
Rectangle left = getNodeByRowColumnIndex(x - 1, y, grid);
|
||||
Rectangle right = getNodeByRowColumnIndex(x + 1, y, grid);
|
||||
rec.setOnMouseClicked(event -> {
|
||||
toggle(rec);
|
||||
toggle(up);
|
||||
toggle(down);
|
||||
toggle(left);
|
||||
toggle(right);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.setTitle("Lights");
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
private void toggle(Rectangle rec) {
|
||||
if(rec == null) {
|
||||
return;
|
||||
}
|
||||
Paint color = rec.getFill();
|
||||
if(color == ON) {
|
||||
rec.setFill(OFF);
|
||||
} else if(color == OFF) {
|
||||
rec.setFill(ON);
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends Node> T getNodeByRowColumnIndex(final int column, final int row, GridPane gridPane) {
|
||||
ObservableList<Node> children = gridPane.getChildren();
|
||||
for(Node node : children) {
|
||||
if(GridPane.getRowIndex(node) == row && GridPane.getColumnIndex(node) == column) {
|
||||
return (T) node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue