Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interesting things, however, unknown? StackPane, animations and filters.

LoaJan 16 2014 — edited Jan 18 2014

EDIT (17/01/2014):


As suggested by jsmith, I changed this thread so we could treat about only one thing. Other questions that existed here before I put each in its proper separate discussion. I hope now we can treat it appropriately, so let's start again:


For the first test case, I tried to create a program that has some controls, and I put a animated background behind it, made with nodes in transition. With such a test, I got the following visual result:


http://s24.postimg.org/tk0psfm2t/programa_errado.jpg

The background is made of white circles that increase and decrease over time asynchronously. Notice how the interface (the controls) is put on top, and it is unfortunately not aligned with respect to the application window. The resulting code seen from the image above can be seen below:


import javafx.animation.Animation;

import javafx.animation.KeyFrame;

import javafx.animation.KeyValue;

import javafx.animation.Timeline;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Pos;

import javafx.scene.Group;

import javafx.scene.Node;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextArea;

import javafx.scene.effect.GaussianBlur;

import javafx.scene.layout.StackPane;

import javafx.scene.layout.VBox;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

import javafx.scene.shape.Rectangle;

import javafx.scene.text.Font;

import javafx.stage.Stage;

import javafx.util.Duration;

public class AnimatedBackground extends Application

{

    // #########################################################################################################

    //                                                                                                      MAIN

    // #########################################################################################################

  

    public static void main(String[] args)

    {

        Application.launch(args);

    }

    // #########################################################################################################

    //                                                                                                INSTANCES

    // #########################################################################################################

  

    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 but_test;

  

    // #########################################################################################################

    //                                                                                                 FX INIT

    // #########################################################################################################

  

    @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 asynchronously.

        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 timeL = new Timeline();

            timeL.getKeyFrames().add(kFrame);

            timeL.setAutoReverse(true);

            timeL.setCycleCount(Animation.INDEFINITE);

            timeL.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.but_test = new Button("CHANGE POSITIONS");

        this.but_test.setAlignment(Pos.CENTER);

      

// Change circles position when button is pressed.

        this.but_test.setOnAction(new EventHandler<ActionEvent>()

        {

            @Override public void handle(ActionEvent e)

            {

                for(Node hexagon : grp_hexagons.getChildren())

                {

                    hexagon.setTranslateX(Math.random() * cenario.getWidth());

                    hexagon.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.but_test);

      

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

    }

}


I believe that my nodes tree is structured as follows:


http://s27.postimg.org/pjpvnlywz/rvore_de_n_s.jpg


I also tried changing the root of my scene graph to StackPane. Unfortunately, the result was bizarre. Despite my interface controls have gone to the middle of the window (which is what I wanted), the background began to animate in a totally weird way. I do not know what else to do about it. It would be strange if this type of feature does not exist in JavaFX, because I could easily do this kind of thing using the Swing library and timming framework. Til this day, I still cannot figure out how I could get around this problem. As a user of JavaFX, I think it would be more interesting if the JavaFX developers would put more cool things similar to this in the documentation to us. Well... If anyone can help me, I'll be really grateful. I'm sure many people can also take advantage of this too.


Thank you for your attention and patience anyway.


Mensagem editada por: Loa

This post has been answered by Loa on Jan 18 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 15 2014
Added on Jan 16 2014
10 comments
7,056 views