Advertisement
Guest User

TransparentExample JavaFX

a guest
Mar 6th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. package edu.setht.fxtests;
  2.  
  3. import javafx.application.Application;
  4. import javafx.scene.Scene;
  5. import javafx.scene.control.Button;
  6. import javafx.scene.layout.Background;
  7. import javafx.scene.layout.StackPane;
  8. import javafx.scene.paint.Color;
  9. import javafx.stage.Stage;
  10. import javafx.stage.StageStyle;
  11.  
  12. public class TransparentExample extends Application{
  13.    
  14.     public static void main(String[] args) {
  15.         launch(args);
  16.     }
  17.    
  18.     public void start(Stage primaryStage) throws Exception {
  19.         StackPane layout = new StackPane();
  20.         Scene root = new Scene(layout, 480, 360);
  21.        
  22.         //BEGIN TRANSPARENT CODE
  23.        
  24.         //make the stage (window) transparent with no decorations
  25.         //  alternatives include DECORATED, UNDECORATED, and UTILITY (and UNIFIED)
  26.         //  http://aquafx-project.com/documentation.html   see <h3>Styling stage</h3>
  27.         //  http://docs.oracle.com/javase/8/javafx/api/index.html?javafx/stage/StageStyle.html
  28.         primaryStage.initStyle(StageStyle.TRANSPARENT);
  29.        
  30.         //set the scene's (container) fill to transparent
  31.         //  it's looking for a Paint object but it seems to accept a Color object instead
  32.         root.setFill(Color.TRANSPARENT);
  33.        
  34.         //finally, apparently the layout itself has a background we need to change
  35.         //  remember that the layout is a child of the scene,
  36.         //  unlike Swing where the layout manager contains the panel
  37.         //  it seems you can do this with a css style method, but I'm not familiar with this yet
  38.         //layout.setStyle("-fx-background-color: transparent;");
  39.         //  so I'm using the setBackground() method instead
  40.         layout.setBackground(Background.EMPTY);
  41.        
  42.         //you can't see the title anywhere, not even in the dock or taskbar
  43.         //  so there's no real point in setting it
  44.         primaryStage.setTitle("Transparent Window");
  45.         primaryStage.setScene(root);
  46.         primaryStage.show();
  47.        
  48.         Button button = new Button("I'm floating!");
  49.         layout.getChildren().add(button);
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement