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)
Related
I'm trying to figure out if there's any way to draw images (drawRect(), drawOval(), etc.) based on if/else statements or by using an ActionListener.
I don't want to post the complete problem because this is for an assignment, but for my own scenario:
If I have a button on a JPanel named "draw rectangle" and I have the x, y, width, and height from user input, is there any way I can attach an actionListener to "draw rectangle" that could somehow draw the rectangle using those values (passed by reference?).
I know I can use paintComponent, but I can't put that into the ActionListener and it seems to do things of its own accord and not based on a specific user's actions.
I don't really have any code for this because I can't figure out how to do it at all.
If I have a button on a
JPanel named "draw rectangle" and I have the
x, y, width, and height from user input, is there any way I can attach
an actionListener to "draw rectangle" that could somehow draw the
rectangle using those values (passed by reference?).
The short answer is yes.
Generally, you'll need to save the rectangle instructions in a model, and have the JPanel redraw the model when any of the shape buttons are pressed.
Let's take your rectangle example. What do you need to know to draw a rectangle on a JPanel? You need a starting point (upper left) and an ending point (lower right). You can use a java.awt.Point to hold the starting and ending point. You can set the thickness of the line, in pixels. You can set the color of the rectangle, using a JColorChooser. You can also set the rectangle to be an outline or filled with the chosen color.
That description will be similar for a line and a triangle. A circle is a little different, with a center point and a radius. As you can see, we already have a lot of information to keep track of just with these simple geometric shapes.
Then there's the issue of the drawing surface itself. It's possible to have a drawing surface larger than your computer screen can show. You can put the drawing surface inside of a JScrollPane.
All of these things must be determined so you can build the model of your application. You do this before you build the view using Swing components and the controller using action listeners.
I am working on a program to visualize an n-body particle simulation. To do this I have created a camera which is used to project particle positions unto the screen. Then, using a spritebatch these particles are rendered in the correct positions. However, for this to work, the origin of the coordinate system must be located at the bottom left of the screen. This is often not the case and instead the origin is located about a fourth of the screen up and to the left in all cases, and this changes when resizing the screen.
How do I guarantee the coordinate system origin is always at the bottom left of the screen, instead of elevated by a few hundred pixels in either axis?
Thank you.
If you decided to use sprite you should stick with proposed workflow and draw them with:
sprite.draw(batch);
I mean, what's the purpose using sprites if you are getting textures and drawing them?
When you draw a texture to screen at position (0,0) it means that it's bottom left corner will be at screen bottom left corner. That' is sprite's "hot-spot" or "origin" is at it's left bottom corner by default.
But you can change that with setOrigin method:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOrigin-float-float-
That is, origin is kinda "center" of the sprite, so i.e. if it rotates it will rotate around that spot. Also when you draw sprite at some coordinates sprites origin will be exactly at those coordinates. That way you don't have to adjust sprite drawing position.
Check on that page what methods you have for sprite class...
I fixed my problem. Instead of using:
sprite.draw(batch);
I used:
batch.setColor(color);
batch.draw(sprite.getTexture(),sprite.getX(),sprite.getY(),sprite.getWidth()*sprite.getScaleX(),sprite.getHeight()*sprite.getScaleY());
I do not know why this worked.
Is there any way I can render sprites, text, etc., but have part of it not rendered so that it is all contained in a rectangle? This would be applied to scroll views and textfields for example.
If it's a screen aligned rectangle, then using a scissor rect is the best solution. See: https://www.khronos.org/opengles/sdk/docs/man/xhtml/glScissor.xml
A fragment shader set to discard any pixels outside of a certain rectangle could work.
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.
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.