Skip to content

Instantly share code, notes, and snippets.

@bplaat
Created February 27, 2018 11:24
Show Gist options
  • Select an option

  • Save bplaat/e1f1b70d75d87c000a3c27c12a7bacbe to your computer and use it in GitHub Desktop.

Select an option

Save bplaat/e1f1b70d75d87c000a3c27c12a7bacbe to your computer and use it in GitHub Desktop.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Test extends Application {
Label getLabel (String text, int fontSize) {
Label label = new Label(text);
label.setStyle("-fx-font-size: " + fontSize + "px; -fx-text-fill: white;");
return label;
}
Button getButton (String text) {
Button button = new Button(text);
button.setPrefSize(200, 50);
button.setStyle("-fx-font-size: 18px;");
return button;
}
public void start (Stage stage) {
int w = 640, h = 480;
StackPane root = new StackPane();
root.setStyle("-fx-background-color: #000;");
AnchorPane container = new AnchorPane();
container.setMinSize(w, h);
container.setMaxSize(w, h);
container.setStyle("-fx-background-color: #29f;");
root.getChildren().add(container);
VBox hbox1 = new VBox(10, getLabel("RedSquareFX", 32), getLabel("Version 1.0", 18));
AnchorPane.setTopAnchor(hbox1, 20d);
AnchorPane.setLeftAnchor(hbox1, 20d);
container.getChildren().add(hbox1);
VBox hbox2 = new VBox(10, getButton("Play"), getButton("High Score"), getButton("Settings"), getButton("Help"), getButton("Credits"));
AnchorPane.setTopAnchor(hbox2, 20d);
AnchorPane.setRightAnchor(hbox2, 20d);
container.getChildren().add(hbox2);
VBox hbox3 = new VBox(10, getLabel("Made by Bastiaan van der Plaat", 18));
AnchorPane.setLeftAnchor(hbox3, 20d);
AnchorPane.setBottomAnchor(hbox3, 20d);
container.getChildren().add(hbox3);
VBox hbox4 = new VBox(10, getButton("Exit"));
AnchorPane.setRightAnchor(hbox4, 20d);
AnchorPane.setBottomAnchor(hbox4, 20d);
container.getChildren().add(hbox4);
stage.setTitle("RedSquareFX");
Scene scene = new Scene(root, w, h);
stage.setScene(scene);
ChangeListener<Number> stageSizeListener = (observable, oldValue, newValue) -> {
System.out.println(scene.getWidth() + "x" + scene.getHeight());
double scale = scene.getWidth() < w * scene.getHeight() / h ? scene.getWidth() / w : scene.getHeight() / h;
container.setScaleX(scale);
container.setScaleY(scale);
};
stage.widthProperty().addListener(stageSizeListener);
stage.heightProperty().addListener(stageSizeListener);
stage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment