Created
August 27, 2014 23:53
-
-
Save jewelsea/c15d86c85c7a0cd6e6b2 to your computer and use it in GitHub Desktop.
Demonstrates adding a halo effect (a drop shadow) around a translucent window.
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
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.*; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.*; | |
import javafx.stage.*; | |
import org.scenicview.ScenicView; | |
// Java 8 code | |
public class ClippedShadow extends Application { | |
private static final int shadowSize = 50; | |
@Override public void start(final Stage stage) { | |
stage.initStyle(StageStyle.TRANSPARENT); | |
StackPane stackPane = new StackPane(createShadowPane()); | |
stackPane.setStyle( | |
"-fx-background-color: rgba(255, 255, 255, 0.5);" + | |
"-fx-background-insets: " + shadowSize + ";" | |
); | |
Scene scene = new Scene(stackPane, 450, 450); | |
scene.setFill(Color.TRANSPARENT); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
// Create a shadow effect as a halo around the pane and not within | |
// the pane's content area. | |
private Pane createShadowPane() { | |
Pane shadowPane = new Pane(); | |
// a "real" app would do this in a CSS stylesheet. | |
shadowPane.setStyle( | |
"-fx-background-color: white;" + | |
"-fx-effect: dropshadow(gaussian, red, " + shadowSize + ", 0, 0, 0);" + | |
"-fx-background-insets: " + shadowSize + ";" | |
); | |
Rectangle innerRect = new Rectangle(); | |
Rectangle outerRect = new Rectangle(); | |
shadowPane.layoutBoundsProperty().addListener( | |
(observable, oldBounds, newBounds) -> { | |
innerRect.relocate( | |
newBounds.getMinX() + shadowSize, | |
newBounds.getMinY() + shadowSize | |
); | |
innerRect.setWidth(newBounds.getWidth() - shadowSize * 2); | |
innerRect.setHeight(newBounds.getHeight() - shadowSize * 2); | |
outerRect.setWidth(newBounds.getWidth()); | |
outerRect.setHeight(newBounds.getHeight()); | |
Shape clip = Shape.subtract(outerRect, innerRect); | |
shadowPane.setClip(clip); | |
} | |
); | |
return shadowPane; | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
please see my SO question...Thank You!
http://stackoverflow.com/questions/42058282/simulate-a-realistic-bounce-in-javafx-animation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer to stackoverflow question: http://stackoverflow.com/questions/25534204/how-do-i-create-a-javafx-transparent-stage-with-shadows-on-only-the-border/.
The tricky thing about this answer is the way it uses shape subtraction on a clip of the shadow effect to ensure that the shadow is only visible around the outlines of the translucent window content (like a halo) and not under the window content itself.