I've been trying to draw a simple figure, however I'm having issues drawing arms. Whenever I use drawRect it just draws a horizontal or a straight rectangle.
I've only tried drawRect since drawLine is not thick enough.
I'm expecting a diagonal rect or maybe a thicker line that could potentially look like an arm.
Related
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)
I am writing a simple game, or so it seemed, that has a functionality of rotating a Rectangle as the mouse moves. This did not seem like a problem at first but it just became a problem. What the Rectangle should do is rotate around a point as the mouse moves around on the Panel.
If you look at the picture, when the mouse moves the Rectangle will rotate. I know you can use the rotate functionality with Graphics2D.
g2d.rotate(angle, centerx, centery);
This is not very help full because I can not get the coordinates of the moving rectangle. This rotates the full graphics! How will I be able to draw this Rectangle so that it does this. I don't have an idea as on how to start. Please help.
Some more code and context would be nice, but based on the current question: You could create a transformed shape. Particularly, Rectangle and Rectangle2D implement the Shape interface. And you can create an AffineTransform that represents the rotation that you are currently doing to your Graphics. So the relevant part of the code should roughly look like
Rectangle2D rectangle = ...
AffineTransform at = AffineTransform.getRotateInstance(
angle, centerx, centery);
Shape rotatedRectangle = at.createTransformedShape(rectangle);
g2d.draw(rotatedRectangle);
You mentioned "collision" in the title. If you intend to use this rectangle in some sort of collision detection, you should note that it is not directly possible to intersect two arbitrary Shape objects. Particulary, you can not intersect the Shape rotatedRectangle with another Shape otherRotatedRectangle, but only with a Rectangle otherRectangle. If this is an issue, you have several options, but this would rather fit into a dedicated question.
I'm developing a game where there is a texture that the player need to move around the screen without touching the borders which are black lines and curves.
The figure rotates while the player moves it and the borders are fixed the entire game.
I need a good method to check a prefect collision between the border and the texture.
I tried to make a rectangle around the texture and check for black and green (texture color) pixels that are near each other inside the rectangle but it's too slow.
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.
Ok
I have the line and the component. The component itself is a JPanel. I draw shapes inside it. And adding the shape to the container. This shape can be any polygon. And drawing a curved line (this can be Line2D, CubicCurve2D or QuadCurve2D) in a container.
Now I need to draw an arrow shape on the container so that while moving a line this arrow would span around the shape.
There can be many curve lines connected to the center of the shape with different Control point. And the shape can be any regular polygon.
At least I need the intersection points of the rounded rectangle and the curve line below image.
Edit: I want to determine the intersection points of a curved line and an arbitrary regular polygon. Given polygon width, height, center points and the starting, ending and the control points of the line.
Edit: I can't post questions so I will edit this one. How can I zoom both JComponents and Graphics object? If it should done with AffineTransform then how should I transform event points on JComponents?
You can use BasicStroke and getStrokedShape(). Set thickness=e.g. 5 and get stroked shapes of the Polygon and Line. Then create 2 Area classes based on the stoked shapes and get intersection Shape.