ParallaxFX: parallax in Java version 2

I’ve added two new controls to FXParallax library. The first and most important one, ParallaxPane, creates the possibility of adding a parallax effect to any Node subclass. The second, AnimatedScrollPane extends ScrollPane by making the scroll animated.

In the FXParallax category you can check out my previous blog posts about this library.

In this post I’m going to describe the mentioned additions to FXParallax.

New version details

ParallaxPane

ParallaxPane is a new control that creates the ability to add a parallax effect to any Node subclass. The API is pretty simple all you have to do is set the Node you’d want to have a parallax effect added to, by calling setContent(Node).

ParallaxPane automatically takes care of what “position” of the clipped content to show depending on the ParallaxPane’s screen position.

The video above shows a demo of the ParallaxPane, two ParallaxPane’s exist in this scene, both containing an ImageView object. As you can see those two ImageView’s have a parallax effect.

These are the images that have been used:

The following code snippet shows an example of how you can define a ParallaxPane, in this case we’re setting an image (through the use of ImageView) as the ParallaxPane’s content:

VBox vBox = new VBox();
ParallaxPane parallaxPane = new ParallaxPane();
String url = ParallaxPaneWithImageTest.class.getResource("small-business.jpg").toExternalForm();
parallaxPane.setContent(new ImageView(url));
vBox.getChildren().add(parallaxPane);

ParallaxPane also has a convenience method that you can use when you simply want to set an Image as its contents. That method is conveniently called setImage, and receives an Image object. You can also use the constructor that receives a Node or the constructor that receives an Image object.

If we wanted to have it defined declaratively, in FXML, it could look like this (using the ParallaxPane convenience constructor with a @NamedArg Image parameter):

<VBox styleClass="main-container">
  <ParallaxPane styleClass="first-image">
    <image>
      <Image url="@small-business.jpg" />
    </image>
  </ParallaxPane>
</VBox>

AnimatedScrollPane

Another detail about the previous demo is that it’s using the new AnimatedScrollPane, a regular ScrollPane could also be used but AnimatedScrollPane adds an extra interesting effect by having the scroll be animated. You’ll also notice that modern applications, like for instance Google Chrome, usually scroll their contents through a subtle animation.

AnimatedScrollPane has the same API as ScrollPane, in fact it extends from ScrollPane. The current limitation is that, for now, it’s implemented with only vertical scroll in mind.

In the future there might be a ScrollPane skin that you can use, so you can add animated scroll to a regular ScrollPane.

Setting a different Node as content of ParallaxPane

As I said above, any Node can be set as the content of ParallaxPane, the following demo shows a video with a parallax effect (the second ParallaxPane contains a video), this is achieved by setting a MediaView as the contents of the ParallaxPane.

And the FXML code snippet that declares the ParallaxPane with the video:

<ParallaxPane styleClass="video">
  <MediaView>
    <mediaPlayer>
      <MediaPlayer autoPlay="true" mute="true">
        <media>
          <Media source="@Swimming_10835.mp4" />
        </media>
        <cycleCount>
          <MediaPlayer fx:constant="INDEFINITE" />
        </cycleCount>
      </MediaPlayer>
    </mediaPlayer>
  </MediaView>
</ParallaxPane>

These demos are all present in the repository, in the test folder.

Conclusion

ParallaxPane and AnimatedScrollPane have been added to the FXParallax library, the former adds the ability of adding a parallax effect to any Node and the other control adds a nice scrolling animation to a ScrollPane like control.

As usual the jar is available for download and through Maven, for details check the FXParallax documentation page, which has been updated.

FXParallax – Parallax library for Java (JavaFX)

I have just released FXParallax, a library for adding parallax effects to Java (JavaFX). In my first post I’ve started to explain what parallax is, where it’s being used and my first implementation of it.

Since my first post, I’ve changed several things: implementation, bugs, API, etc.

FXParallax is composed of a set of controls that are either already ready to use and have a parallax effect or are a general purpose control that makes it easy for you to add a parallax effect to a UI component.

Currently only one control exists in this library: ParallaxListView, it’s a ListView with a parallax effect.

I’ve added a page with documentation for this library, you should check it out here.

Future developments

I plan to add a general purpose control, probably named ParallaxPane. It will allow you to more easily create a UI component with a parallax effect. It will accept a collection of layers and will probably allow some way of defining the scroll speed.

Unless there is a high demand I won’t be doing this in the near future. There are other things I’d like to do first.

New Parallax control for Java (JavaFX)

Introduction

Parallax is a visual effect where you combine at least two layered images moving at different speeds to achieve a sense of depth.

Think about when you’re driving on the road and you see the trees close to you moving fast whereas the trees that are further away will move in the same direction but slower, the result is that you perceive the trees that are moving faster to be closer to you and the others to be further.

If you are a fan of video games you’ve certainly seen this effect before: two or more images are placed one over the other, the background moves slower, and the foreground, which could be the player’s avatar or the foreground where he stands moves faster.

This is an often used trick in the video games world. It started to be used quite long ago, an example is the Super Mario video game series.

Super Mario game using a parallax effect

This effect has been adopted in the user interface area, where you can see it used on web sites and also some applications.

Parallax on a web site – source: https://dribbble.com/shots/1378010–GIF-Parallax-Building-animation

My goal was to bring this to JavaFX in the form of a control, where you could, through a simple API, add a parallax effect.

Parallax in a ListView

My first objective was to bring this to the ListView, that is, have a control with an API similar to the ListView where you could set the background image and the list of items just like in the JavaFX ListView. This control would be named the ParallaxListView.

Implementing ParallaxListView

To have a parallax effect the items in the ListView, that is, the cells, need to scroll with the background but at a faster rate.

This new control will use the JavaFX ListView with a transparent background, layered in front of a background image. The background image will be inside a ScrollPane.

The first challenge is that the scroll of the JavaFX ListView is done through jumps, that is, when you do a scroll action the position will jump from the original position to the end position without going through the intermediate position values. This will break the effect, since there is no movement like in the real world, only a discrete set of positions. In the real world the objects in the foreground move faster through a translation animation, remember this is about mimicking real world phenomenon like the driving example I gave earlier, to give a sense of depth.

So to accomplish this I had to implement my own ListView scrolling as well as the background scrolling, which when scrolling animates through a set of positions. The following is a live capture turned into a GIF of an example application using the ParallaxListView, the live version image quality and animation is better:

ParallaxListView example (image quality and animation worse than live version)

Where to get it

For now you can get it here: https://github.com/dukke/fxparallax.

I decided to stick with Java 8 as Java 9 is still relatively new and some people might have not yet migrated. This way Java 8 and Java 9 programs can use it.

I’m using private API, namely VirtualFlow, but this has turned into public API in JavaFX 9 so it will be easy to migrate the ParallaxListView code.

The test folder has examples of how to use the control.

You have to scroll through the mouse wheel to see the effect.

Conclusion and Further Developments

The ParallaxListView can give a nice touch to your application, adding a bit of flair, however don’t overuse this effect.

In the next days I plan to add the following:

  • Add support for scrolling through the Scroll Bar.
  • Add API to change the height of the background image in relation with the foreground
  • Add the ability to specify the orientation of the ParallaxListView: Hoizontal or Vertical
  • Add a documentation page
  • Also provide a general purpose control that lets you specify a background and an arbitrary scrollable foreground control.

Update: This blog post has been updated in 16 April 2018.

 

Update to FXRibbon (Ribbon for Java)

I’ve updated the Ribbon control and its documentation.

Here’s some of the relevant code changes (you can check the git log history for more details):

  • Cleaned up API
  • Cleaned up code into standard conventional packages
  • Fixed and cleaned up test apps (that also serve as documentation examples)
  • Added gradle build and configuration
  • Other refactors

Here’s the new page for the FXRibbon documentation: here.

Future developments for this library:

  • Add button controls: current javafx buttons don’t provide the best API for the ribbon use case, there’s also an issue where if you have a button with a multi line label, the label won’t be vertically aligned with the rest of the buttons.
  • Add other specialized controls, e.g. Ribbon gallery
  • Fix other issues in the issue tracker.