Custom Component: Starter with hover effect

I was looking for a starter component for to use on the dock like main entry page of our application. It should have a hover effect on mouse enter/ mouse exit and a start effect on mouse click.

As JavaFX has already very useful Transition implementations I gave the ScaleTransition a try. I wanted a smooth expand and shrink effect on mouse enter and exit. Unfortunately transitions can on be played backwards. Reverse play is only supported when using the auto reverse option combined with an appropriate cycle count.

But I saw that changing the properties of an existing ScaleTransition instance and playFromStart() is a working approach:

        setOnMouseEntered(new EventHandler()
        {
            @Override
            public void handle(MouseEvent t)
            {
                scaleTransition.setFromX(getScaleX());
                scaleTransition.setFromY(getScaleY());
                scaleTransition.setToX(expandToMaxProperty.get());
                scaleTransition.setToY(expandToMaxProperty.get());
                scaleTransition.playFromStart();
            }
        });

        setOnMouseExited(new EventHandler()
        {
            @Override
            public void handle(MouseEvent t)
            {
                scaleTransition.setFromX(getScaleX());
                scaleTransition.setFromY(getScaleY());
                scaleTransition.setToX(1);
                scaleTransition.setToY(1);
                scaleTransition.playFromStart();
            }
        });

The start effect played on mouse click is a combination of both sequential and parallel played transitions: scale and fade out are played in parallel and in the end the starter goes back to an initial position:

        fadeTransition = new FadeTransition(Duration.millis(200), this);
        fadeTransition.setCycleCount(1);
        fadeTransition
                .setInterpolator(Interpolator.EASE_BOTH);
      
        startTransition = new ParallelTransition();
        startTransition.setCycleCount(2);
        startTransition.setAutoReverse(true);
        startTransition.getChildren()
                .addAll(scaleTransition,
                fadeTransition);

        initTransition = new ScaleTransition(Duration.millis(200), this);
        initTransition.setToX(1);
        initTransition.setToY(1);
        initTransition.setCycleCount(1);
        initTransition
                .setInterpolator(Interpolator.EASE_BOTH);

        startsSequentialTransition = new SequentialTransition();
        startsSequentialTransition.getChildren()
                .addAll(
                startTransition,
                initTransition);

Whole Starter code

public class Starter extends StackPane
{

    private ParallelTransition startTransition;
    private ScaleTransition scaleTransition;
    private ScaleTransition initTransition;
    private FadeTransition fadeTransition;
    private SequentialTransition startsSequentialTransition;
    private DoubleProperty expandToMaxProperty;
    private ImageView baseImage;

    public Starter(String base, double fitWidth, double fitHeight)
    {
        baseImage = new ImageView(base);
        init(fitWidth, fitHeight);
    }

    public Starter(Image base, double fitWidth, double fitHeight)
    {
        baseImage = new ImageView(base);
        init(fitWidth, fitHeight);
    }

    private void init(double fitWidth, double fitHeight)
    {
        expandToMaxProperty = new SimpleDoubleProperty(1.2);

        baseImage.setFitWidth(fitWidth);
        baseImage.setFitHeight(fitHeight);

        Color selectedColor = Color.rgb(0, 0, 0, 0.5);
        DropShadow dropShadow = DropShadowBuilder.create()
                .color(selectedColor)
                .build();
        setEffect(dropShadow);

        dropShadow.radiusProperty()
                .bind(scaleXProperty()
                .multiply(8));
        dropShadow.offsetXProperty()
                .bind(scaleXProperty()
                .multiply(8));
        dropShadow.offsetYProperty()
                .bind(scaleYProperty()
                .multiply(8));

        scaleTransition =
                new ScaleTransition(Duration.millis(200), this);
        scaleTransition.setCycleCount(1);
        scaleTransition
                .setInterpolator(Interpolator.EASE_BOTH);



        fadeTransition = new FadeTransition(Duration.millis(200), this);
        fadeTransition.setCycleCount(1);
        fadeTransition
                .setInterpolator(Interpolator.EASE_BOTH);

        startTransition = new ParallelTransition();
        startTransition.setCycleCount(2);
        startTransition.setAutoReverse(true);
        startTransition.getChildren()
                .addAll(scaleTransition,
                fadeTransition);

        initTransition =
                new ScaleTransition(Duration.millis(200), this);
        initTransition.setToX(1);
        initTransition.setToY(1);
        initTransition.setCycleCount(1);
        initTransition
                .setInterpolator(Interpolator.EASE_BOTH);

        startsSequentialTransition = new SequentialTransition();
        startsSequentialTransition.getChildren()
                .addAll(
                startTransition,
                initTransition);

        getChildren()
                .addAll(baseImage);

        setOnMouseEntered(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                scaleTransition.setFromX(getScaleX());
                scaleTransition.setFromY(getScaleY());
                scaleTransition.setToX(expandToMaxProperty.get());
                scaleTransition.setToY(expandToMaxProperty.get());
                scaleTransition.playFromStart();
            }
        });

        setOnMouseExited(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                scaleTransition.setFromX(getScaleX());
                scaleTransition.setFromY(getScaleY());
                scaleTransition.setToX(1);
                scaleTransition.setToY(1);
                scaleTransition.playFromStart();
            }
        });

        setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent t)
            {
                scaleTransition.setFromX(getScaleX());
                scaleTransition.setFromY(getScaleY());
                scaleTransition.setToX(2);
                scaleTransition.setToY(2);
                fadeTransition.setFromValue(1.0f);
                fadeTransition.setToValue(0.5f);
                startsSequentialTransition.playFromStart();
            }
        });
    }

    public DoubleProperty expandToMaxProperty()
    {
        return expandToMaxProperty;
    }
}

[ U P D A T E 21-10-2014]

Made some modifications to get the Starter running on Java 8:

public class Starter extends StackPane
{

    private ParallelTransition startTransition;
    private ScaleTransition scaleTransition;
    private ScaleTransition startScaleTransition;
    private ScaleTransition initTransition;
    private FadeTransition fadeTransition;
    private SequentialTransition startsSequentialTransition;
    private DoubleProperty expandToMaxProperty;
    private final ImageView baseImage;

    public Starter(String base, double fitWidth, double fitHeight)
    {
        baseImage = new ImageView(base);
        init(fitWidth, fitHeight);
    }

    public Starter(Image base, double fitWidth, double fitHeight)
    {
        baseImage = new ImageView(base);
        init(fitWidth, fitHeight);
    }

    private void init(double fitWidth, double fitHeight)
    {
        expandToMaxProperty = new SimpleDoubleProperty(1.2);

        baseImage.setFitWidth(fitWidth);
        baseImage.setFitHeight(fitHeight);

        Color selectedColor = Color.rgb(0, 0, 0, 0.5);
        DropShadow dropShadow = DropShadowBuilder.create()
                .color(selectedColor)
                .build();
        setEffect(dropShadow);

        dropShadow.radiusProperty()
                .bind(scaleXProperty()
                .multiply(8));
        dropShadow.offsetXProperty()
                .bind(scaleXProperty()
                .multiply(8));
        dropShadow.offsetYProperty()
                .bind(scaleYProperty()
                .multiply(8));

        scaleTransition =
                new ScaleTransition(Duration.millis(200), this);
        scaleTransition.setCycleCount(1);
        scaleTransition
                .setInterpolator(Interpolator.EASE_BOTH);

        startScaleTransition =
                new ScaleTransition(Duration.millis(200), this);
        startScaleTransition.setCycleCount(1);
        startScaleTransition
                .setInterpolator(Interpolator.EASE_BOTH);

        fadeTransition = new FadeTransition(Duration.millis(200), this);
        fadeTransition.setCycleCount(1);
        fadeTransition
                .setInterpolator(Interpolator.EASE_BOTH);

        startTransition = new ParallelTransition();
        startTransition.setCycleCount(2);
        startTransition.setAutoReverse(true);
        startTransition.getChildren()
                .addAll(startScaleTransition, fadeTransition);

        initTransition =
                new ScaleTransition(Duration.millis(200), this);
        initTransition.setToX(1);
        initTransition.setToY(1);
        initTransition.setCycleCount(1);
        initTransition
                .setInterpolator(Interpolator.EASE_BOTH);

        startsSequentialTransition = new SequentialTransition();
        startsSequentialTransition.getChildren()
                .addAll(
                startTransition,
                initTransition);

        getChildren()
                .addAll(baseImage);

        setOnMouseEntered((MouseEvent t) -> {
            scaleTransition.setFromX(getScaleX());
            scaleTransition.setFromY(getScaleY());
            scaleTransition.setToX(expandToMaxProperty.get());
            scaleTransition.setToY(expandToMaxProperty.get());
            scaleTransition.playFromStart();
        });

        setOnMouseExited((MouseEvent t) -> {
            scaleTransition.setFromX(getScaleX());
            scaleTransition.setFromY(getScaleY());
            scaleTransition.setToX(1);
            scaleTransition.setToY(1);
            scaleTransition.playFromStart();
        });

        setOnMouseClicked((MouseEvent t) -> {
            startScaleTransition.setFromX(getScaleX());
            startScaleTransition.setFromY(getScaleY());
            startScaleTransition.setToX(2);
            startScaleTransition.setToY(2);
            fadeTransition.setFromValue(1.0f);
            fadeTransition.setToValue(0.5f);
            startsSequentialTransition.playFromStart();
        });
    }

    public DoubleProperty expandToMaxProperty()
    {
        return expandToMaxProperty;
    }
}

Find complete code repo at BitBucket.

5 thoughts on “Custom Component: Starter with hover effect

    • Thanks for quick responce. Just small typo correction:

      setOnMouseClicked() should also use ‘scaleTransition’ and not startScaleTransition.

      BTW, please have a look into you mailbox, I’ve asked you somthing there. Thanks.

      • Well “scaleTransition” is basically not a typo that’s the fix ;-)! As it complains “Cannot stop when embedded in another animation” I tought it might be a good idea to create a dedicated scaleTransition for the start effect.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>