Results 1 to 20 of 20
  1. #1
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Javafx - Transitions

    Hi Everyone.
    I am facing a problem with transition.setOnFinished() method.

    Any thing I place in that handler keeps looping. I would only like to execute once after transition.play() ends.
    How do I stop stop the looping?

    I have a few other problems but I like begin to solve simple things first.

    Thanks for any help from anyone.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    Moved from New to Java.

    To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    Java FX Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package pathtest;
    
    import java.util.ArrayList;
    import javafx.animation.TranslateTransition;
    import javafx.application.Application;
    import static javafx.application.Application.launch;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    
    /**
     *
     * @author Ghansham
     */
    public class PathTest extends Application {
        
        ArrayList<Ball> position = new ArrayList();
    
        TranslateTransition translation;
    TranslateTransition newTranslation;
      StackPane root = new StackPane();
    
        @Override
        public void start(Stage primaryStage) {
          
            
    
    
    
        
    Ball ball1 = new Ball(Color.RED);
    Ball ball2 = new Ball(Color.GREEN);
    Ball ball3 = new Ball(Color.BLUE);
    ball1.setFy(-250);
    ball1.setTx(50);
    
    ball2.setFy(-210);
    ball2.setTx(100);
    
    ball3.setFy(-180);
    ball3.setTx(150);
    position.add(ball1);
    position.add(ball2);
    position.add(ball3);
    
    int i = 0;
    while (i<position.size()){
        Ball temp = position.get(i);
       translation = new TranslateTransition(Duration.millis(5000),temp.getCircle());
         
          translation.setFromY(temp.getFy());
         translation.setToX(temp.getTx());
     
        root.getChildren().add(temp.getCircle());
       newTranslation = translation;
         translation.play();
         i++;
         
    }
         translation.setOnFinished(new EventHandler<ActionEvent>(){
     
                @Override
                public void handle(ActionEvent arg0) {
                       int j=0;
                    Ball k;
                       k = position.get(0);
                  //  if (j==0){
                     
             
                
                      newTranslation.setToY((k.getTy()+40));
                      newTranslation.play();
             //   }
              //      else System.out.println("END");
                        
                }  
              }); 
     
            Scene scene = new Scene(root, 800, 650);
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        
       
     
        public static void main(String[] args) {
            launch(args);
        }
    }
    Ball is really a class that defines a circle.
    In a nutshell 3 balls with existing X and Y coordinates are stored in an array list.
    They then translate with the existing coordinate.
    The first ball (red ball) then translate to a new set of coordinate which begins at line 68.
    But when the code compile and run the last ball in the arraylist gets translated regardless which ball you choose.
    Also it keeps repeating over and over.

    Any help would be appreciated.
    Also thanks for the prompt response.
    Last edited by ghansham; 07-04-2013 at 04:31 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    Did you read the SSCCE page I linked to?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    Quote Originally Posted by DarrylBurke View Post
    Did you read the SSCCE page I linked to?

    db
    Yes I did but that is the shortest way I can demonstrate the problem. Line 39-52 just store the balls in an arraylist.

    The while loop traverse through the arraylist and translate the balls in it.
    The problem I am having is between line 68-85, where the setOnFinish() method keeps repeating.

    In this method I add new X and Y positions to the existing balls.

    I don't really know how to make it more short.
    See the video of what the code does.
    https://www.dropbox.com/s/n6lfdgac60...transition.mp4

    Thanks for the response.
    Last edited by ghansham; 07-04-2013 at 04:32 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    How do we compile the code without the Ball class?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    Sorry my bad.
    Here it is.

    Java FX Code:
    class Ball {
        
       private Circle circle;
       private Color color;
       
       private int fx, fy, tx, ty;
    
       
               
        public Ball(Color ballColor) {
           color = ballColor;
         circle = new Circle(100, 100, 20); 
           
             circle.setFill(color);
        }
        
        public Circle getCircle() {
            return circle;
           }
    
        public void setFx(int fx) {
            this.fx = fx;
        }
    
        public void setFy(int fy) {
            this.fy = fy;
        }
    
        public void setTx(int tx) {
            this.tx = tx;
        }
    
        public void setTy(int ty) {
            this.ty = ty;
        }
        
        
    
        public int getFx() {
            return fx;
        }
    
        public int getFy() {
            return fy;
        }
    
        public int getTy() {
            return ty;
        }
    
        public int getTx() {
            return tx;
        }
        
        
    }
    Last edited by ghansham; 07-04-2013 at 04:30 PM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    Why does your Ball class have an instance field of type Ball?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    1. You really need to use meaningful variable and method names. It's counter-intuitive that getBall() returns a Circle, and variable names like g and rtt tell you nothing about what they are used for.

    2. Since you set the onFinished handler after the loop, it is only set to the last Transition.

    3. Since you invoke play() in the onFinished handler, the transition repeats. What else did you expect?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    You'll probably want rtt (please give it a meaningful name) to be a final local variable, and get rid of the field g (same remark).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    Also, I would give the Ball class a constructor that accepts parameters for all its fields -- not just for the color.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    Quote Originally Posted by DarrylBurke View Post
    1. You really need to use meaningful variable and method names. It's counter-intuitive that getBall() returns a Circle, and variable names like g and rtt tell you nothing about what they are used for.
    My apologies, I change the names.


    2. Since you set the onFinished handler after the loop, it is only set to the last Transition.
    So If I want to move the second ball ( arrayPosition.get(1) ) in the onFinished handler, how do I do that?

    3. Since you invoke play() in the onFinished handler, the transition repeats. What else did you expect?
    How do I play the new transition without it repeating?
    I tried surround it with a conditional IF & Else but that repeats as well.

    Thanks alot for looking at my code.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    Quote Originally Posted by ghansham View Post
    So If I want to move the second ball ( arrayPosition.get(1) ) in the onFinished handler, how do I do that?
    Huh? Do you mean that you want to move the balls one by one, and stop after all three have been moved?

    Describe your requirement in simple sentences.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    I want to continue moving the balls after the first transition ends with new coordinates.
    e.g. The first transition moves the balls to point A. I would like to then move the balls from point A to point B, by either one by one or all moves together.
    I hope this helps to explain what I want to do.

    The code I post is just a breakdown of what I want to do.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    And after the Ball reaches point B, then what?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    It opens a new dialog box and that will be the end of the transition.

  17. #17
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    To give an overview of what I want to do is, I want to demonstrate multiplication in moving circles in groups.
    e.g. 6 x 2 = 12,
    1. An arraylist will store the 12 circles with its coordinates, then it transitions to a panel.
    2. It would then transition the first 6 circles out of the 12 into new coordinates.
    3. Then it transition the second 6 circles and into new coordinates ends.

    The link below will give you a powerpoint slide to demonstrate.
    Power Point Slide Link

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    So, something like this?
    Java FX Code:
    import java.util.ArrayList;
    import javafx.animation.TranslateTransition;
    import javafx.application.Application;
    import static javafx.application.Application.launch;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class PathTest extends Application {
    
      @Override
      public void start(Stage primaryStage) {
    
        StackPane root = new StackPane();
        Color[] colors = {Color.RED, Color.GREEN, Color.BLUE};
        for (int i = 0; i < colors.length; i++) {
          final Ball ball = new Ball(colors[i], -250 + 30 * i, 50 + 50 * i);
          final TranslateTransition transition =
                  new TranslateTransition(Duration.millis(5000), ball.getCircle());
          transition.setFromY(ball.getFy());
          transition.setToX(ball.getTx());
          root.getChildren().add(ball.getCircle());
          transition.play();
    
          transition.setOnFinished(new EventHandler<ActionEvent>() {
            boolean secondMove = false;
    
            @Override
            public void handle(ActionEvent arg0) {
              if (!secondMove) {
                transition.setToY((ball.getTy() + 40));
                transition.play();
                secondMove = true;
              }
            }
          });
        }
    
        Scene scene = new Scene(root, 800, 650);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    
    class Ball {
    
      private Circle circle;
      private int fx, fy, tx, ty;
    
      public Ball(Color color, int fy, int tx) {
        this(color, 0, fy, tx, 0);
      }
    
      public Ball(Color color, int fx, int fy, int tx, int ty) {
        circle = new Circle(100, 100, 20, color);
        this.fx = fx;
        this.fy = fy;
        this.tx = tx;
        this.ty = ty;
      }
    
      public Circle getCircle() {
        return circle;
      }
    
      public int getFx() {
        return fx;
      }
    
      public void setFx(int fx) {
        this.fx = fx;
      }
    
      public int getFy() {
        return fy;
      }
    
      public void setFy(int fy) {
        this.fy = fy;
      }
    
      public int getTx() {
        return tx;
      }
    
      public void setTx(int tx) {
        this.tx = tx;
      }
    
      public int getTy() {
        return ty;
      }
    
      public void setTy(int ty) {
        this.ty = ty;
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  19. #19
    ghansham is offline Member
    Join Date
    Jul 2013
    Location
    South America, Guyana
    Posts
    12
    Rep Power
    0

    Default Re: Javafx - Transitions

    Thanks A Lot. That answer all my questions I had, I am able to move forward on my application.

    Also I have another issue with combo box. After adding a Text object to a combobox the item in the list is not visible after selecting it twice. With the other item in the picture, the same happens and the entire list is not visible.
    When you use strings without the Font style everything is fine.

    Java FX Code:
    Text text1 = new Text("Text 1");
            text1.setFont(Font.font ("Verdana",FontWeight.BOLD, 20));
            Text text2 = new Text("Text 2");
            text2.setFont(Font.font ("Verdana",FontWeight.BOLD, 20));
            
            ComboBox combo = new ComboBox();
            combo.getItems().addAll(text1,text2);


    Javafx - Transitions-combobox.png

    Once again thanks for all your help, I really appreciate it.

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Javafx - Transitions

    Quote Originally Posted by ghansham View Post
    I have another issue ...
    Start a new thread (with an SSCCE).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-18-2013, 12:10 AM
  2. JavaFX 2.0 and/or Swing
    By JavaPete in forum Forum Lobby
    Replies: 2
    Last Post: 10-25-2012, 10:36 AM
  3. Help with javaFX?!
    By JavaNoob!1 in forum JavaFX
    Replies: 7
    Last Post: 07-18-2012, 11:56 PM
  4. Mysql and JavaFX
    By cjr9968 in forum JDBC
    Replies: 11
    Last Post: 01-29-2010, 03:51 AM
  5. JavaFX Script and JavaFX Mobile
    By levent in forum Java Software
    Replies: 1
    Last Post: 01-27-2010, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •