javafx - Setting the background of a png image

774 views Asked by At

I have an ImageView that is made of a png image (transparent background). I want to change the color of the image continuously. I can change the color using JFXFillTransition method in the jfoenix library. The constructor of that method requires javafx.scene.layout.Region, so it can't be applied on javafx.scene.Image.ImageView directly. I am using a VBox and adding the ImageView in that VBox. Obviously the VBox's width doesn't wrap the content (here ImageView) by default. This gives a solution of wrapping content. But group can't be used as an argument of JFXFillTransition here. As a result of this, if I run the code the color of the VBox changes but that is not confined to the ImageView (as the VBox is larger than the image). So, how can I shrink the size of the VBox so that it wraps the image only (any other pane suitable for this is also ok)?

Code Snippet:

VBox openingImageBox = new VBox();
    openingImageBox.setAlignment(Pos.CENTER);
    openingImageBox.getChildren().add(openingImageView);
    borderPane.setTop(openingImageBox);


    //Transition On ImageView
    JFXFillTransition transition1 = new JFXFillTransition(Duration.millis(1000),openingImageBox,Color.VIOLET,Color.INDIGO);
    JFXFillTransition transition2 = new JFXFillTransition(Duration.millis(1000),openingImageBox,Color.INDIGO,Color.BLUE);
    JFXFillTransition transition3 = new JFXFillTransition(Duration.millis(1000),openingImageBox,Color.BLUE,Color.GREEN);
    JFXFillTransition transition4 = new JFXFillTransition(Duration.millis(1000),openingImageBox,Color.GREEN,Color.YELLOW);
    JFXFillTransition transition5 = new JFXFillTransition(Duration.millis(1000),openingImageBox,Color.YELLOW,Color.ORANGE);
    JFXFillTransition transition6 = new JFXFillTransition(Duration.millis(1000),openingImageBox,Color.ORANGE,Color.RED);
    JFXFillTransition transition7 = new JFXFillTransition(Duration.millis(1000),openingImageBox,Color.RED,Color.VIOLET);
    transition1.play();
    transition1.setOnFinished(e->
    {
        transition2.play();
    });
    transition2.setOnFinished(e->
    {
        transition3.play();
    });
    transition3.setOnFinished(e->
    {
        transition4.play();
    });
    transition4.setOnFinished(e->
    {
        transition5.play();
    });
    transition5.setOnFinished(e->
    {
        transition6.play();
    });

    transition6.setOnFinished(e->
    {
        transition7.play();
    });
    transition7.setOnFinished(e->
    {
        transition1.play();
    });

enter image description here

0

There are 0 answers