Results 1 to 9 of 9
Thread: JavaFX move node (x,y)
- 11-07-2017, 07:23 PM #1
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 5
JavaFX move node (x,y)
Java FX Code:Rectangle grass = new Rectangle(440,40);
There is stuff like
Java FX Code:grass.setTranslateY(100); grass.setLayoutY(20);
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); }
- 11-08-2017, 12:30 PM #2
Senior Member
- Join Date
- Sep 2014
- Location
- MA, USA
- Posts
- 399
- Rep Power
- 7
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?
- 11-08-2017, 02:53 PM #3
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 5
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);
Last edited by asdfg; 11-08-2017 at 02:56 PM.
- 11-08-2017, 03:24 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 11-08-2017, 03:33 PM #5
Senior Member
- Join Date
- Sep 2014
- Location
- MA, USA
- Posts
- 399
- Rep Power
- 7
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.
- 11-08-2017, 09:50 PM #6
Senior Member
- Join Date
- Nov 2016
- Posts
- 163
- Rep Power
- 5
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); } }
- 11-09-2017, 01:43 AM #7
Senior Member
- Join Date
- Sep 2014
- Location
- MA, USA
- Posts
- 399
- Rep Power
- 7
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.
- 11-09-2017, 02:41 AM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 11-09-2017, 02:45 PM #9
Senior Member
- Join Date
- Sep 2014
- Location
- MA, USA
- Posts
- 399
- Rep Power
- 7
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 );
Similar Threads
-
Gui con JavaFX
By Julik in forum JavaFXReplies: 5Last Post: 11-27-2016, 06:12 PM -
JavaFX Book | Learn JavaFX More In Depth
By SnakeDoc in forum JavaFXReplies: 0Last Post: 05-18-2013, 12:10 AM -
Problem casting from org.w3c.dom.Node to my subclass of Node
By msphelix in forum Advanced JavaReplies: 1Last Post: 08-12-2012, 04:15 AM -
mapping one XML node to another XML node in GUI.
By ashu_i20 in forum XMLReplies: 1Last Post: 04-16-2012, 03:29 PM -
JavaFX Script and JavaFX Mobile
By levent in forum Java SoftwareReplies: 1Last Post: 01-27-2010, 05:48 PM
Bookmarks