Drawing Triangular Grid

Andrew Tran
Greenhorn
Posts: 6
I am currently doing an assignment that involves Ehrenstein Illusions and I am kind of stuck on trying to draw a Triangle(the triangle on the right of the picture with 6 circles) where the squares are connected side by side. Any clues/help on how i can achieve this? Attached is a picture of what that triangular shape would look like.  Thanks in advance.

drawgrid.png

Piet Souris
Rancher
Posts: 1402
29
• 1
Hi Andrew,

I think you should first create a square with the circles inside, as one figure. A Path2D object seems ideal for such a figure. It also has the advantage that you can use a coordinate system that is as comfortable as possible.

For instance, create a Path2D.Double,  and add the biggest square, with coördinates (-1,-1), (1,1) et cetera,  or other coördinates you like. Next, you know the diameter of your largest circle. Add it, and go on until you are satisfied. Now you have your basic figure. Look at the Shape and Path2D api how easy it is to transform these things. What is also necessary is a decent knowledge of coordinate system when it comes to drawing these things in a panel, at the required size. See for more information the Graphics2D api.

If this all sounds too complex, you can also draw your basic figure in a BufferedImage, thát you can also rotate and collect for your big triangle

Well, my initial thoughts.

Campbell Ritchie
Sheriff
Posts: 50666
83
I think you can draw your diamond‑and‑circle with radius r and repeat it r to the right and r up. The sides of the diamond will be r×√2.
Then you can draw your diamond‑and‑circle with radius r and repeat it r to the right and r down.
Then you can ...

Fred Kleinschmidt
Bartender
Posts: 381
3
The answer depends on which triangle you are talking about. It is pretty easy to figure out the triangle that connects the centers of the outer circles.
If you want the smallest triangle that completely encloses all of the circles, then the sides of the triangle have length ( 2 * r * ( 2 + sqrt(3) ) )

Andrew Tran
Greenhorn
Posts: 6
I think i got them to display similar to the assignment, but I have another question.
Where would i put the g.setColor and g.fillOval in order to make the lines Black and fill the circle with Green?
Initially, i thought i would put it in the drawDisc method where i tried
g.setColor(Color.BLACK);
g.setColor(Color.GREEN);

It seems like only the lines get changed to black, but nothing is filled into the circles.

Thanks

this is my current code:

Piet Souris
Rancher
Posts: 1402
29
In  the code in your opening post, in the drawSubfigure-method, you have the correct sequence  already. You can use that same code in your drawDisc method., as far as I can tell.

Fred Kleinschmidt
Bartender
Posts: 381
3
You should really consider using Swing instead of AWT. Then you would create you own JPanel subclass, and do all of the drawing in its paintComponent() method.