Sun's implementation provides some custom Java 2D shapes like Rectangle, Oval,
Polygon etc. but it's not enough. There are GUIs which require more custom
shapes like Regular Polygon, Star and Regular polygon with rounded corners. The project provides some more shapes often used. All the classes
implements Shape interface which allows user to use all the usual methods of
Graphics2D like fill(), draw(), and create own shapes by combining them.
The shape represent regular polygon with fixed vertex count. Regular polygon
is based on circle with center and radius. An additional parameter is start
angle. The angle defines a first vertex location in other words it's an angle
between X axis and a line which intersects circle. By default the angle is 0.
The constructors of RegularPolygon class are following:
Where: x and y are coordinates of the Regular polygon center, r is the radius of the polygon circle, vertexCount is count of polygon vertexes (e.g. 6 for hexagon), startAngle is the rotation angle where the first vertex is set (default 0). More complicated model of polygon is a star. User passes vertex count,
star center point and two radiuses for inner and outer circle. Outer one
defines size of star and the inner circle defines how acute the star branches
are.
The constructors of RegularPolygon class are following:
Where: x and y are coordinates of the Regular polygon center, r is the radius of the outer circle, innerR is the radius of the inner circle, vertexCount is count of polygon vertexes (e.g. 6 points star), startAngle is the rotation angle where the first vertex is set (default 0). The shape is based on existing polygon but all the corners are rounded. The arcWidth parameter defines a distance from corner point where the arc starts.
The image illustrates how arcWidth size definition in a polygon's angle.
RoundPolygon class obtains any existing polygon and generates a shape with rounded corners according to arcWidth parameter passed. The constructor of RoundPolygon class is following:
For more info how to use the shapes see source code of the TestApplication. I am going to add some more custom shapes. Please suggest yours and they could be implemented once. The source code is included in the .jar file (See src directory). Click Downloads to get Shapes library, demo application and source code. Mirror stanislavl.blogspot.com to discuss or post a comment. |
||||||||