Can't move a rectangle in JavaFX? - java

I can't seem to move a rectangle object in JavaFX. I've placed the rectangle inside of a stackpane, but whenever I change the coordinates of the rectangle, it won't move (even if I change the coordinates of initialization). So,
Rectangle rect = new Rectangle(150,150,75,75);
will be in the center of the stackpane, as well as
Rectangle rect = new Rectangle(2043,136,75,75);
Whenever I use
rect.setX();
The rectangle doesn't move at all. I know I'm missing something really simple, I just can't figure it out. I don't want to use a transition, because my goal is to simply move the rectangle a few pixels in the direction of an arrow key press. So what exactly am I doing wrong?

Solution
Use a different container type (e.g. a Pane) if you want to manually specify layout co-ordinates (e.g. the x,y co-ordinates of a Rectangle).
Background
A StackPane is a managed layout pane - it controls the layout of the items you place in it (by default centering items in the stack). So it doesn't matter what co-ordinates you give the Rectangle, when you place it in the StackPane, the layout manager will move your rectangle so that it is in the center of the stack.
JavaFX has two concepts for positioning, one is layout co-ordinates, the other is translation deltas, which are added to the layout co-ordinates. TranslateTransitions work by modifying translation deltas. Translation is meant for animations and temporarily moving things around. Translation is independent of and does not effect layout values, so you could place something in a StackPane and apply a TranslateTransition to it and it would still move, but it would move from the center of the stack, as that is the initial layout position.

Related

HoverProperty inside canvas in javafx-8

I want to draw a line in javafx-8 canvas such that when I keep my mouse over it, it should change its color and glow (if possible).
How do I do this?
Sorry, you can't do this.
In javafx, every Node is mouse aware, that is, you can track MouseEvent on a Node, but, unfortunately, drawings inside a Canvas aren't Node, rather they are mapping of pixels of that Canvas.
Alternative :
You can use an AnchorPane instead of canvas and have Line , Circle as its children.

Javafx center shape on screen inside Group

I made a sketch with the behavior I'm trying to get.
Sketch
So I want to display a rectangle in the middle of the screen and a triangle that covers part of the rectangle. The problem is, when I use a Stackpanethe rectangle is centered but the triangle as well and I can't move it to the bottom right position. When I use a Groupit is not centered. Is there any way to get my intended behavior?
Set alignment of your triangle. Use static setAlignment(Node child, Pos value) method:
StackPane.setAlignment(triangle, Pos.BOTTOM_RIGHT)

Drawing clickable grid with background

I am currently exploring my options for drawing (javafx) limited 2D (N x N) grid where crossing are clickable elements.
Example: (red rectangles are example of clickable areas - meaning area is bound to particular intersection, all crossings should be such rectangle).
Requirements:
Grid is resizable (x/y scale is preserved).
"Crossings" can receive mouse events.
As of now it's either make GridPane that will hold rectangles that receive events. Each rectangle would have background drawing corresponding to its position in grid (so rectangles on border have different background).
Other things I've considered is making transparent rectangles placed on GridPane that itself draws background.
Now my question here is - what would be best/proper way of doing such thing. What tools can be useful to me?

Rotate in Z JavaFX

I have a image added with scene builder. It has a viewport with this characteristics: 400,400 300x300. As you can imagine there're a part of the image that isn't showing. I want to move the image but all. Rotate it in Z. So, the part that now isn't visible becomes visible.
For example, see that attitude indicator (my project is also an attitude indicator):
The background is rotated. But the image is bigger than you can see, so although it rotates you do't see white parts.
How can I do that??
What you actually want to do is rotate around a pivot point (in your case the center of the screen).
http://download.java.net/jdk8/jfxdocs/javafx/scene/transform/Rotate.html

Java - Resizing a Drawn Object

Let's say that I have drawn a square on the screen using the following code:
I want that if the user clicks on the shape, a border with four small boxes (north, south, east and west) is drawn on the shape. Hovering on any of the small boxes changes the cursor to the resize cursor and, if the user drags the small box to a new location, the shape is resized.
How can this be done please?
Instead of using drawRect() method create Shape object for the rectangle and draw the Shape. For Shape you can use getBounds() to get main rectangle. Use the bounds to find the small boxes rectangles (also Shapes).
When mouse is moved (or dragged) check whether one of the boxes shapes contains the mouse event coordinates.
On drag you can change original Shape using AffineTransfrorm.

Categories

Resources