How to place rectangle in grid order? - java

So far, my program draws many rectangles and the locations of the rectangles are random (not nice). How can I place the rectangle in grid order so that it look appealing?

Use a grid manager like GridLayout

Related

Is a GridBagLayout the best way to make a panel like this?

I'm storing some x,y coordinates in the rectangles (JTextFields) and I'd like the JLabels to be in the middle of those two text fields.
Idea:

JavaFX: Setting a border within a rectangle to keep the width and height

So, I have a grid of rectangles in my scene and I want to give it a border to visually seperate it to the others. I am currently using grid.setGridLinesVisible(true); and that works fine, but I am sure this isn't supposed to be used for that.
I tried setting a border with nodes[j][i].setStroke(Color.BLACK); and this works too, but now my whole grid is getting a lot bigger because it's drawing the border on the outside of the rectangle and therefore resizing it.
Is there a way to draw a border/stroke inside of a rectangle to keep the transformation?
I already looked up the documentation but there isn't such a function.
Thanks for the help!
You can use setStrokeType for your rectangles to draw the border inside using StrokeType.Inside:
nodes[j][i].setStrokeType(StrokeType.Inside);

Circle Border with Different Arc Colors

How to make a circle border with arcs of two colors like following picture?
I'm using canvas and the thing which I started by creating separate arcs of different color and then combining them together like this.
But this greatly increases the complexity of the app. Additionally, I have to create multiple circles and rotate them frame by frame once the app starts. With so many different arcs, everything gets messy.
Is there a nicer way of doing this? Is it possible to represent the circle as a single entity?

Changing the border color in polygon

Using Polygon class, I have created a triangle.
By using setStroke(Color.AQUA), I have changed the border color of that polygon. Triangle has 3 borders. How to set different color for every border?
There is no predefined method in Polygon for giving each border an individual color. You have to implement your own class or use a combination of 3 independent lines to make triangle.

Finding Coordinates on a Bufferred Image

I have a buffered image of a 4x4 checker Board (resolution 400x400) rendered on half a JPanel. Is it possible to find the coordinates of each square corner without doing it manually? I'm using absolute positioning on the JPanel and it is the only container besides the Frame
If you have a 4 x 4 checkerboard that's 400 x 400, then each square is 100 x 100 pixels.
When you construct a BufferedImage like this, you save a Rectangle for each square as you're doing the construction.
That way, when you do a mouse click later, you can use the contains method of Rectangle to determine which square was clicked.
You shouldn't use absolute positioning. If your checker board takes up half the JPanel, FlowLayout or BoxLayout works well.

Categories