Can a rectangle act as a panel? - java

I would like to draw rectangles on another rectangle as shown in the figure. So, when I move the rectangle1 or rectangle2, the other rectangles should also move with it. Is there a way to draw rectangles in this way?

Assuming that the outer rectangle represents a selection rectangle meant to select the enclosed rectangles, you can maintain a List<Rectangle> and update the position of each rectangle as the mouse is dragged. A complete example using List<Node> is cited here.

Related

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)

How corner points of a shape displayed

I try to write a program to draw a custom shapes and then execute all mouse events like dragging,clicking,moving etc.I want to know that in other editor when any shape is select or mouse is near to there boundary then its boundary point start to display.I just want the logic not code how corner points displayed like in image? I've done checking that my mouse clicking is inside of shape or not.This is a rounded-rectangle.When I clicked on its rectangle boundary is start displaying and connection points are also start displaying.How do I do that?
Shape interface has getBounds() and contains() methods. Use contains() to determine whether point belongs to the Shape and then use getBounds() to get rectable and use the rectangle's corners to draw the dragging points.

Polygon inside polygon in libgdx and box2d

I'm new in libgdx and box2d and I would like to know if there's an easy way to put a polygon randomly inside the bounds of another polygon.
EDIT
I want something like this:
Where the black polygon could be positioned in any other zone inside the green polygon and never outside.
There is no possibility of easy placing one polygon inside another in Box2D. Solution is shown on the drawing below:
Yellow rectangles is the first body, green - second. Each yellow rectangle is separate fixture.

Java - Draw Square Around Circle

I have a program containing five buttons:
i) Square ii) Rectangle iii) Circle iv) Triangle v) Clear
Clicking on any of the first four buttons will draw the respective shape on the drawing canvas using the following code:
The shapes can be dragged. They also should be resized. I was just trying to change the cursor of the mouse around the four edges of the shape (NW, NE, SW, SE). This works fine for squares and rectangles since they have four sides. However, I have some problem implementing the same for the circle and triangle.
This is what I did in my mouseClicked event:
What I want to do is that if the string shape_type contains circle, for example, it draws a border around it so that the user can see the boundaries of the shape.
However, I can't just use the following code inside the if statement:
How can I draw the border please? Thanks :)
You are correct that you can't do painting in a MouseListener method. What you can do is set shapeUnderMouse in your mouseClicked method, then call repaint. You'll get better performance if you only repaint the areas that you know are changing visually, but it's not strictly necessary.
The if (shapeUnderMouse != null) block should be placed in your paint method.

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