2

I am trying to simulate the effect one would get from this css example:

border-radius: 50%;

From searching the API and reading posts on forums including this one, I found that I should be using -fx-background-radius. This however is not giving me the wanted effect.

I setup a picture as the background using -fx-background-image:url(...) and then I want to make it into a circle.

How can I achieve this?

Update

So I see that I was not being too specific so let me try to elaborate:

I created a Pane object, that does extend the Region class from JavaFX.

main.fxml:

...
<Pane styleClass="wrapper">
    <Pane layoutX="34.0" layoutY="28.0" styleClass="image" />
</Pane>

For this pane I created the styleclass image as seen above.

main.css:

.list-contact .image {
  -fx-alignment:left;
  -fx-pref-height:40px;
  -fx-pref-width:40px;
  -fx-background-radius:50%;
  -fx-background-image:url(...);
  -fx-background-repeat:no-repeat;
}

The effect I get:

The effect I get:

The effect I want:

The effect I want

I hope this explains it better.

miniwolf
  • 339
  • 1
  • 4
  • 19
  • 1
    Can you explain this a bit more? What does `border-radius: 50%;` do? Do you have an example image that you could embed in your question? Do you just want to create a background image which is clipped to a circle (using JavaFX CSS)? Do you want to change the shape of the entire region in which the background image is displayed, or leave a square region with a round background? – jewelsea Jun 24 '16 at 02:28
  • Possible duplicate of [How to make a javafx button with circle shape of 3xp diameter?](http://stackoverflow.com/questions/26850828/how-to-make-a-javafx-button-with-circle-shape-of-3xp-diameter) – Oliver Jan Krylow Jun 24 '16 at 06:56
  • 1
    @OJKrylow: Nope, `ImageView` is not a `Region` like a `Button`... – fabian Jun 24 '16 at 10:19

3 Answers3

3

This is not possible from CSS alone, since ImageView does not support any of Region's CSS properties.

However you can use a Ellipse as clip for the ImageView:

@Override
public void start(Stage primaryStage) throws MalformedURLException {
    Image img = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Space_Needle_2011-07-04.jpg/304px-Space_Needle_2011-07-04.jpg");
    ImageView iv = new ImageView(img);
    Ellipse ellipse = new Ellipse(img.getWidth() / 2, img.getHeight() / 2, img.getWidth() / 2, img.getHeight() / 2);
    iv.setClip(ellipse);

    Text text = new Text("Space Needle, Seattle, Washington, USA");
    StackPane.setAlignment(text, Pos.TOP_CENTER);

    StackPane root = new StackPane(text, iv);

    Scene scene = new Scene(root, img.getWidth(), img.getHeight());
    scene.setFill(Color.AQUAMARINE);

    primaryStage.setScene(scene);
    primaryStage.show();
}

I know it doesn't look good to let the image cover the text. This is only done for the purpose of demonstration.

fabian
  • 80,457
  • 12
  • 86
  • 114
2

It looks like a CSS border-radius: 50% should create an elliptical border, and JavaFX CSS does support the % shorthand for either -fx-border-radius or -fx-background-radius. To get the desired effect, however, use Path.subtract() to create an elliptical matte for the image, as shown below.

image

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;

/**
 * @see http://stackoverflow.com/a/38008678/230513
 */
public class Test extends Application {

    private final Image IMAGE = new Image("http://i.imgur.com/kxXhIH1.jpg");

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Test");
        int w = (int) (IMAGE.getWidth());
        int h = (int) (IMAGE.getHeight());
        ImageView view = new ImageView(IMAGE);
        Rectangle r = new Rectangle(w, h);
        Ellipse e = new Ellipse(w / 2, h / 2, w / 2, h / 2);
        Shape matte = Path.subtract(r, e);
        matte.setFill(Color.SIENNA);
        StackPane root = new StackPane();
        root.getChildren().addAll(view, matte);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

In one Line with Circle as Clip.You can use setClip(any shape).:

imageView.setClip(new Circle(width,height,radius);

The width,height,radius have to be slighty smaller that ImageView size to work.

Inspired by GuiGarage web site.

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93