Skip to content
Advertisement

Trying to create a scrolling background effect with imageViews in javafx

Im trying to create a scrolling background effect with two imageViews where one picture is on top of another picture and out of the window; then i try to scroll both down the window to create a scrolling effect by changing their y coordinates. I made a loop to do so and put a thread.sleep so it wouldnt do it too quickly. Then i reset the picutres positions and do the loop again. However, when i try to run the program, the window will never open. Taking out the loop obviously properly shows the window with the picutre.

JavaScript

Advertisement

Answer

Your loop is not the right thing to do. Use a Transition Animation on each ImageView. You want a TranslateTransition.

Something like this:

JavaScript

If your intention is to have these images as a parallax background that scrolls “forever”, set the transitions to cycle indefinitely

JavaScript

and use slightly different durations for each.

If you are using the same image, don’t load it twice:

JavaScript

Here’s the whole start method with an added speed slider, just for fun:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement