• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Kaleidoscope project

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to make a project that will draw a kaleidoscope. I would need to draw some shapes inside a triangle and then reflect that triangle in six directions to make a hexagonal display.

I don't know how to draw shapes within a triangle so that any part of the shape that extends beyond the boundary is not drawn and I don't know how to reflect the shapes to make a hexagon.

Can you give me any tips please?
 
Bartender
Posts: 5637
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a Graphics2D (for instance from a BufferedImage) you can give it a clip (see the api) and that clip can be any shape, like a triangle. Anyrhing you draw in that G2D will be clipped inside that shape.
 
S Connor
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.  Do you know how I could rotate or reflect the image somehow?
 
Saloon Keeper
Posts: 28713
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Connor wrote:Thanks.  Do you know how I could rotate or reflect the image somehow?


Graphics2d has a translation function. I think that's what you use in such cases. Also for scaling.
 
Piet Souris
Bartender
Posts: 5637
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, my first thoughts would be:

1) determine the size of your equilateral triangle, say W and H.

2) create the triangle as a Path2D.Double, with coordinates (W/2, 0), (0, H), (W, H) and close it.

3) create a BufferedImage buf, type INT_ARGB, with width W and height H

4) get its g2d, set the triangle as clip and draw all the colours and shapes to it that you want.

5) create the main BufferedImage mainBuf, with height 2*H, and sufficient width. We now draw 6 times buf to its g2d, the first one at (0, H), then rotate mainBuf PI/3 around its center (as Tim said, there are methods in the g2d that make this possible) and draw buf again, at (), H). By doing this six times, mainBuf has now 6 copies of the triangle, all neatly rotated around mainBuf's center.

6) draw mainBuf in your JPanel

7) global health warning: I typed this out of my head, and therefore there are no guarantees! If it doesn't work like I had in mind, let us know what problem you emcountered.
 
Piet Souris
Bartender
Posts: 5637
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example of what I described. It is a very rough version, though
 
reply
    Bookmark Topic Watch Topic
  • New Topic