• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Graphics

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need code snippets for small application which draws an �n-sided� regular polygon. Allow the user of the program to select the number of sides of the polygon up to a limit of 20, and to set the color
from a pallette of 12 colors consisting of the 3 primary, 3 secondary, and 6 tertiary colors.
I don't know how to start. Help will be appreciated.
Thanks.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I'd start by considering the implications of different numbers of sides on the polygon object. The inner angles between all sides will add up to 360 degrees, so you need to divide this by the number of sides the user wants in order to find the angle at the end of each side. Now you can start by drawing one line section of length x, perhaps as a horizontal line. Then using trigonometry ratios you can work out the end coordinates of the next line section and so on based on the desired length and the angle of the join. Hopefully at the end they will meet up!
Once you have this polygon object created you can use the fill graphics method using whatever color you want.
One important point is that Java works in radians, not degrees.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the sum of the inner angles for an n-sided polygon equals (n-2)*180
 
reply
    Bookmark Topic Watch Topic
  • New Topic