Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# Drawing Triangular Grid

Greenhorn
Posts: 7
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

Rancher
Posts: 2829
96
• 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.

Marshal
Posts: 60127
188
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 ...

Bartender
Posts: 620
9
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: 7
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: 2829
96
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: 620
9
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.

 It is sorta covered in the JavaRanch Style Guide.