JavaFX 2.X - Animated background and animated controls


Question

A few days ago I started studying JavaFX, and came across the desire to perform 2 experiments. Firstly, I would like to know if it is possible to put an animated background behind an user interface. I've succeeded in creating an animated background, and now I'm having great difficulties to position some controls in the middle of my interface.

I'd like to introduce you 2 pictures of my program. The first demonstrates the undesirable result that I'm getting:

Wrong result.

I believe this is my nodes tree:

Nodes tree

This is the code of my application:

public class AnimatedBackground extends Application
{
// #########################################################################################################
//                                                                                                      MAIN
// #########################################################################################################

public static void main(String[] args) 
{
    Application.launch(args);
}

// #########################################################################################################
//                                                                                                INSTÂNCIAS
// #########################################################################################################

private Group root;
private Group grp_hexagons;
private Rectangle rect_background;
private Scene cenario;

// UI

private VBox lay_box_controls;

private Label lab_test;
private TextArea texA_test;
private Button bot_test;

// #########################################################################################################
//                                                                                                 INÍCIO FX
// #########################################################################################################

@Override public void start(Stage stage) throws Exception 
{
    this.confFX();

    cenario = new Scene(this.root , 640 , 480);

    this.rect_background.widthProperty().bind(this.cenario.widthProperty());
    this.rect_background.heightProperty().bind(this.cenario.heightProperty());

    stage.setScene(cenario);
    stage.setTitle("Meu programa JavaFX - R.D.S.");
    stage.show();
}

protected void confFX()
{
    this.root = new Group();
    this.grp_hexagons = new Group();

    // Initiate the circles and all animation stuff.
    for(int cont = 0 ; cont < 15 ; cont++)
    {
        Circle circle = new Circle();
        circle.setFill(Color.WHITE);
        circle.setEffect(new GaussianBlur(Math.random() * 8 + 2));
        circle.setOpacity(Math.random());
        circle.setRadius(20);

        this.grp_hexagons.getChildren().add(circle);

        double randScale = (Math.random() * 4) + 1;

        KeyValue kValueX = new KeyValue(circle.scaleXProperty() , randScale);
        KeyValue kValueY = new KeyValue(circle.scaleYProperty() , randScale);
        KeyFrame kFrame = new KeyFrame(Duration.millis(5000 + (Math.random() * 5000)) , kValueX , kValueY);

        Timeline linhaT = new Timeline();
        linhaT.getKeyFrames().add(kFrame);
        linhaT.setAutoReverse(true);
        linhaT.setCycleCount(Animation.INDEFINITE);
        linhaT.play();
    }

    this.rect_background = new Rectangle();

    this.root.getChildren().add(this.rect_background);
    this.root.getChildren().add(this.grp_hexagons);

    // UI

    this.lay_box_controls = new VBox();
    this.lay_box_controls.setSpacing(20);
    this.lay_box_controls.setAlignment(Pos.CENTER);

    this.bot_test = new Button("CHANGE POSITIONS");
    this.bot_test.setAlignment(Pos.CENTER);

    this.bot_test.setOnAction(new EventHandler<ActionEvent>() 
    {
        @Override public void handle(ActionEvent e) 
        {
            for(Node hexagono : grp_hexagons.getChildren())
            {
                hexagono.setTranslateX(Math.random() * cenario.getWidth());
                hexagono.setTranslateY(Math.random() * cenario.getHeight());
            }
        }
    });

    this.texA_test = new TextArea();
    this.texA_test.setText("This is just a test.");

    this.lab_test = new Label("This is just a label.");
    this.lab_test.setTextFill(Color.WHITE);
    this.lab_test.setFont(new Font(32));

    this.lay_box_controls.getChildren().add(this.lab_test);
    this.lay_box_controls.getChildren().add(this.texA_test);
    this.lay_box_controls.getChildren().add(this.bot_test);

    this.root.getChildren().add(this.lay_box_controls);
}
}

I've tried to make the use of a StackPane as the root of my scene graph, but also found an undesired result. Despite the controls have stayed in the center of the window, the circles begin to move in as they grow and shrink, making it appear that everything is weird.

The second thing I would like to know is if it is possible to customize the controls so they perform some animation when some event happens. Although we can change the appearance of controls using CSS, it's harder to create something complex. For example, when a control changes its appearance due to a change of state, the transition state change is not made in an animated way, but in an abrupt and static way. Is there a way to animate, for example, a button between its states? This would be done using the JavaFX API? Or would that be using CSS? Or would not be possible in any way?

Thank you for your attention.

1
2
1/13/2014 2:53:22 AM

Accepted Answer

after much struggle, I and some users of the Oracle community could resolve this issue. I see no need to repeat here all the resolution made ​​by us, so I'll post the link so you can access the solution of the problem. I hope this benefits us all. Thanks for your attention anyway.

https://community.oracle.com/thread/2620500

0
1/19/2014 1:44:00 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon