package fx; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.binding.DoubleBinding; import javafx.beans.property.ObjectProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.property.SimpleObjectProperty; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Bounds; import javafx.geometry.Point2D; import javafx.scene.Cursor; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ScrollPane; import javafx.scene.effect.DropShadow; import javafx.scene.effect.Reflection; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.FlowPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.stage.Window; import javafx.util.Duration; public class UndecoratedMinSize2 extends Application { private ScrollPane scrollPane; private ObjectProperty anchor = new SimpleObjectProperty(null); private SimpleDoubleProperty widthStage = new SimpleDoubleProperty(); private SimpleDoubleProperty heightStage = new SimpleDoubleProperty(); public static void main(String[] args) { launch(args); } @Override public void start(final Stage primaryStage) { Pane mainContainer = initstage(primaryStage); mainContainer.getChildren().addAll( createButton()); primaryStage.show();// Show the primaryStage } private Pane initstage(Stage primaryStage) { primaryStage.setTitle("Hello Java"); primaryStage.initStyle(StageStyle.TRANSPARENT); /* The minWidth or minHeight is for the stage "DECORATED", it has on effect to the stage non "DECORATED". */ primaryStage.setMinWidth(600); primaryStage.setMinHeight(400); final FlowPane mainContainer = new FlowPane(); mainContainer.setStyle( "-fx-background-color: white; " + "-fx-background-radius: 10px; " + "-fx-padding: 10px;" + "-fx-hgap: 30px; " + "-fx-vgap: 50px;"); scrollPane = new ScrollPane(); scrollPane.setStyle("-fx-background-radius: 10px;"); scrollPane.setContent(mainContainer); scrollPane.setPannable(true); mainContainer.prefWidthProperty().bind(new DoubleBinding() { { super.bind(scrollPane.viewportBoundsProperty()); } @Override protected double computeValue() { Bounds bounds = scrollPane.getViewportBounds(); if (bounds == null) { return 0; } else { return bounds.getWidth(); } } }); AnchorPane root = new AnchorPane(); root.setStyle( "-fx-border-color: black; " + "-fx-border-width: 1px; " + "-fx-border-radius: 10px; " + "-fx-background-color: rgba(0, 0, 0, 0); " + "-fx-background-radius: 10px;"); DropShadow dropShadow = new DropShadow(); dropShadow.setRadius(20.0); // dropShadow.setSpread(0.2); root.setEffect(dropShadow); enableDragging(root); root.getChildren().add(scrollPane); AnchorPane.setTopAnchor(scrollPane, 0.0); AnchorPane.setRightAnchor(scrollPane, 10.0); AnchorPane.setBottomAnchor(scrollPane, 10.0); AnchorPane.setLeftAnchor(scrollPane, 10.0); AnchorPane superRoot = new AnchorPane(); superRoot.getChildren().add(root); AnchorPane.setTopAnchor(root, 10.0); AnchorPane.setRightAnchor(root, 10.0); AnchorPane.setBottomAnchor(root, 10.0); AnchorPane.setLeftAnchor(root, 10.0); Scene scene = new Scene(superRoot, 950, 650, Color.TRANSPARENT); // scene.getStylesheets().add(getClass().getResource("controls.css").toExternalForm()); primaryStage.setScene(scene); return mainContainer; } /** * Enable the root node to be dragged. Realize some drag functions. * * @param root - the root node */ private void enableDragging(final AnchorPane root) { /* This mouse event is for resizing window. Define some specific cursor patterns. */ root.setOnMouseMoved(new EventHandler() { public void handle(MouseEvent me) { if ((me.getX() > root.getWidth() - 10 && me.getX() < root.getWidth() + 10) && (me.getY() > root.getHeight() - 10 && me.getY() < root.getHeight() + 10)) { root.setCursor(Cursor.SE_RESIZE); } else if (me.getX() > root.getWidth() - 5 && me.getX() < root.getWidth() + 5) { root.setCursor(Cursor.H_RESIZE); } else if (me.getY() > root.getHeight() - 5 && me.getY() < root.getHeight() + 5) { root.setCursor(Cursor.V_RESIZE); } else { root.setCursor(Cursor.DEFAULT); } } }); /* when mouse button is pressed, save the initial position of screen. */ root.setOnMousePressed(new EventHandler() { public void handle(MouseEvent me) { Window primaryStage = root.getScene().getWindow(); anchor.set(new Point2D(me.getScreenX() - primaryStage.getX(), me.getScreenY() - primaryStage.getY())); widthStage.set(primaryStage.getWidth()); heightStage.set(primaryStage.getHeight()); } }); /* when mouse button is released, clear the initial position of screen. */ root.setOnMouseReleased(new EventHandler() { public void handle(MouseEvent me) { anchor.set(null); } }); /* when screen is dragged, translate it accordingly. */ root.setOnMouseDragged(new EventHandler() { public void handle(MouseEvent me) { if (anchor.get() != null) {// The drag event on the root really takes place. Window primaryStage = root.getScene().getWindow(); if (root.getCursor() == Cursor.H_RESIZE) { primaryStage.setWidth(widthStage.get() + (me.getScreenX() - (anchor.get().getX() + primaryStage.getX()))); } else if (root.getCursor() == Cursor.V_RESIZE) { System.err.println("resize " + (heightStage.get() + (me.getScreenY() - (anchor.get().getY() + primaryStage.getY())))); primaryStage.setHeight(heightStage.get() + (me.getScreenY() - (anchor.get().getY() + primaryStage.getY()))); } else if (root.getCursor() == Cursor.SE_RESIZE) { primaryStage.setWidth(widthStage.get() + (me.getScreenX() - (anchor.get().getX() + primaryStage.getX()))); primaryStage.setHeight(heightStage.get() + (me.getScreenY() - (anchor.get().getY() + primaryStage.getY()))); } else {// moving the stage primaryStage.setX(me.getScreenX() - anchor.get().getX()); primaryStage.setY(me.getScreenY() - anchor.get().getY()); } } } }); } /** * Define a button * @return a button */ private Node createButton() { Button button = new Button(); button.setEffect(new Reflection()); button.setText("Say 'Hello Java'"); button.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println("Hello Java!"); } }); /* add an animation effect */ Timeline timeline = new Timeline(); timeline.setCycleCount(Timeline.INDEFINITE); timeline.setAutoReverse(true); timeline.getKeyFrames().addAll( new KeyFrame(Duration.ZERO, new KeyValue(button.opacityProperty(), 1.0)), new KeyFrame(new Duration(5000), new KeyValue(button.opacityProperty(), 0.0))); timeline.play(); /* set CSS style */ button.setStyle( // "-fx-font: 14px 'Cambria'; " + "-fx-text-fill: #006464; " + "-fx-background-color: #e79423; " + "-fx-background-radius: 20.0; " + "-fx-padding: 5.0;"); return button; } }