Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By benji2505

Thread: JavaFX move node (x,y)

  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    2

    Default JavaFX move node (x,y)

    Java FX Code:
    Rectangle grass = new Rectangle(440,40);
    Is there way to move(push) Rectangle to x, y several times?

    There is stuff like
    Java FX Code:
    grass.setTranslateY(100);
    grass.setLayoutY(20);
    But they will push to new location once, if parameters isnt changed.

    Im looking for a method who would move (push) my rectangle as much as i call method.

    i.e.

    Java FX Code:
    //this  will move my rectangle 100px only once. But I want to move 300px;
    for (int i = 0; i < 3; i++ )
    {
    grass.setLayoutY(100);
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    394
    Rep Power
    5

    Default Re: JavaFX move node (x,y)

    I am not sure , what you are trying to accomplish here. It sounds to me that you want a node to be visibly moving when you touch a button or something. Like moving the gun right and left in space invaders. Is that correct?

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    2

    Default Re: JavaFX move node (x,y)

    I attempt to program water physics. Its important when I need to scale water and make it falldown.

    Of course I can use TranslateTransition(instead) with no problems. But it would be very comfortable to call method, not changing anything, and just push 20pixels my picture.

    I.e. I have decided to Translate( move) my rectangle to x 20pixels:
    Java FX Code:
    grass.setTranslateX(20);
    //Later I need to Translate again.
    grass.setTranslateX(20);
    //and so on...
    grass.setTranslateX(20);
    But no matter how many times I call this method ( unless I change parameters). It will stay in the same position after first call. Because .setTranslate method sets global coordinates, not counting by current position.
    Last edited by asdfg; 11-08-2017 at 01:56 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: JavaFX move node (x,y)

    Would you please provide a small, compilable program that demonstrates the problem. Use the Rectangle move as an an example.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    394
    Rep Power
    5

    Default Re: JavaFX move node (x,y)

    bug in code: yourNode.setTranslateX(20); moves the node to x-position of 20 pixels. If you call it multiple times, it always places it at the 20 pixel location (same spot). You have to vary the parameter that you feed into the setTranslate() method.

  6. #6
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    2

    Default Re: JavaFX move node (x,y)

    Java FX Code:
    import javafx.animation.AnimationTimer;
    import javafx.application.Application;
    import javafx.geometry.Bounds;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    
    public class JAVAXx extends Application {
    	
    	 int x = 0;
    	 int y = 100;
        
    	@Override
        public void start(Stage primaryStage) 
        {   		
         Rectangle rec1 = new Rectangle (40,40);
         rec1.setFill(Color.DARKGRAY);
         rec1.setTranslateX(200);
    	
         
         Rectangle rec2 = new Rectangle (40,40);
         rec2.setFill(Color.DARKCYAN);
    
         
         Rectangle myHero = new Rectangle (50,50);
         myHero.setTranslateY(100);
         
         Button b1 = new Button ("->");
         b1.setTranslateX(300);
         b1.setTranslateY(280);
         b1.setOnAction ( e->{   
        	 x += 20;
        	 myHero.setTranslateX(x);     
         });
         
         Button b2 = new Button ("<-");
         b2.setTranslateX(260);
         b2.setTranslateY(280);
         b2.setOnAction ( e->{   
        	 x -= 20;
        	 myHero.setTranslateX(x);     
         });
         
         Button b3 = new Button ("Up");
         b3.setTranslateX(330);
         b3.setTranslateY(320);
         b3.setOnAction ( e->{   
             y -= 20;
             myHero.setTranslateY(y);     
             });
         
         Button b4 = new Button ("Down");
         b4.setTranslateX(330);
         b4.setTranslateY(360);
         b4.setOnAction ( e->{   
             y += 20;
             myHero.setTranslateY(y);     
             });
         
      	 	  	  	
    	 Group multipleComponents = new Group();	
    	 multipleComponents.getChildren().addAll(rec1, rec2, myHero, b1, b2, b3, b4);
         multipleComponents.prefHeight(100); // WHAT IS purpose of this method? I can rec1.seWidth(0 and rec1.setHeight for nodes. But I cant to do this for Group !
    
          
         AnimationTimer animator = new AnimationTimer()
    	   {
    	        public void handle (long arg0) 
    	        {				            		    		
    	        	 Bounds obstacle = rec1.localToScene( rec1.getBoundsInLocal() );
    	             Bounds obstacle2 = rec2.localToScene( rec2.getBoundsInLocal() );	
    	             
    	             Bounds myself = myHero.localToScene( myHero.getBoundsInLocal() );	
    	      
    		            if ( myself.intersects(obstacle) )
    		            {
    		            	/*of course I could create new Integer at 16 line, and keep plus'ing here
    		            	 *But image if there is much more elements, working with arrayList can get confusing.
    		            	 */
    		            	rec1.setTranslateX(20);     
    		            }
    		            
    		            if ( myself.intersects(obstacle2) )
    		            {
    		            	rec2.setTranslateX(20);     
    		            }		            
    		            		                       
    	       }  
    	      	        
    	    };	
    	
    	 animator.start();  	
          
         Scene scene = new Scene(multipleComponents, 600, 400);
         primaryStage.setScene(scene);
         primaryStage.show();
        
    
        }
            
        public static void main(String[] args) 
        {
            launch(args);
        }
    }

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    394
    Rep Power
    5

    Default Re: JavaFX move node (x,y)

    So, you are asking “what should I do”?
    I am guessing that in lines 83 and 88 above you want to move the nodes 20 pixels to the right if they intersect with an obstacle.
    So, you have to do something like rec.setTranslateX(rec.getdafrigginxposition + 20).
    That’s about as close as I can come to the wildly disrespected spoonfeeding here.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: JavaFX move node (x,y)

    I think he fixed it using setTranslate(). I guess I need to get up to speed on JavaFX. I am not used to animating objects outside of the paintComponent method of javax.swing.JPanel. The translation for nodes apparently is different than say for java.awt.Polygon which iterates across all points and adds the offset.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    394
    Rep Power
    5

    Default Re: JavaFX move node (x,y)

    setTranslateX(20) moves a node to the position 20 pixels right of the 0,0 location on the respective pane, whereby the 0,0 point is the top left pixel. If OP wants to move it 20 pixels further right in case of intersection with another node OP has to do something like yourNode.setTranslateX(yourNode.getTranslateX()+20 );
    asdfg likes this.

Similar Threads

  1. Gui con JavaFX
    By Julik in forum JavaFX
    Replies: 5
    Last Post: 11-27-2016, 05:12 PM
  2. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-17-2013, 11:10 PM
  3. Replies: 1
    Last Post: 08-12-2012, 03:15 AM
  4. Replies: 1
    Last Post: 04-16-2012, 02:29 PM
  5. JavaFX Script and JavaFX Mobile
    By levent in forum Java Software
    Replies: 1
    Last Post: 01-27-2010, 04: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
  •