javafx - Border-Radius and Shadow on ImageView -
i want apply border-radius , shadow in javafx.
in css3 be:
box-shadow: rgba(0,0,0,0.8) 0 0 10px; border-radius: 3px;
now want in javafx, border-radius not working in javafx scene builder. here screenshot of problem:
javafx screenshot http://rapid-img.de/images/b92e2112.jpg
on screenshot can see use:
-fx-border-radius: 10 10 10 10; -fx-background-radius: 10 10 10 10;
use following css drop shadow:
-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0);
see javafx css reference guide details.
to border in addition drop shadow, place imageview containing image in stackpane. , apply effect css above stackpane, in addition background , padding on stackpane.
for example css below applied stackpane containing imageview provide red border around image:
-fx-padding: 10; -fx-background-color: firebrick;
if want background defining border curved @ edges, use:
-fx-background-radius: 5;
that gets image below image enclosed in shadowed border:
if want round image itself, it's bit trickier. need apply code to:
- clip image rounded rectangle.
- snapshot clipped image.
- store snapshot image in imageview.
- remove clip imageview.
- apply drop shadow effect imageview.
then can below:
some code "batmanlost.java":
import javafx.application.application; import javafx.fxml.*; import javafx.scene.*; import javafx.scene.effect.dropshadow; import javafx.scene.image.*; import javafx.scene.layout.pane; import javafx.scene.paint.color; import javafx.scene.shape.rectangle; import javafx.stage.stage; import java.io.ioexception; public class batmanlost extends application { class wingclipper { @fxml private imageview imageview; @fxml public void initialize() { // set clip apply rounded border original image. rectangle clip = new rectangle( imageview.getfitwidth(), imageview.getfitheight() ); clip.setarcwidth(20); clip.setarcheight(20); imageview.setclip(clip); // snapshot rounded image. snapshotparameters parameters = new snapshotparameters(); parameters.setfill(color.transparent); writableimage image = imageview.snapshot(parameters, null); // remove rounding clip our effect can show through. imageview.setclip(null); // apply shadow effect. imageview.seteffect(new dropshadow(20, color.black)); // store rounded image in imageview. imageview.setimage(image); } } public static void main(string[] args) { launch(args); } @override public void start(stage stage) throws ioexception { fxmlloader loader = new fxmlloader( getclass().getresource( "batmanlostinthemix.fxml" ) ); loader.setcontroller(new wingclipper()); pane batman = loader.load(); stage.settitle("where's batman?"); stage.setscene(new scene(batman)); stage.show(); } }
with fxml "batmanlostinthemix.fxml":
<?xml version="1.0" encoding="utf-8"?> <?import javafx.scene.image.image?> <?import javafx.scene.image.imageview?> <?import javafx.scene.layout.anchorpane?> <anchorpane id="anchorpane" maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="313.0" prefwidth="477.0" style="-fx-background-color: azure;" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"> <children> <imageview fx:id="imageview" layoutx="29.0" layouty="44.0" fitheight="224.0" fitwidth="400.0" pickonbounds="true" preserveratio="true"> <image> <image url="http://collider.com/wp-content/uploads/lego-batman-movie-dc-super-heroes-unite-1.jpg" /> </image> </imageview> </children> </anchorpane>
Comments
Post a Comment