Articles Projects Tips Downloads Contacts About

Java2D Shapes project (Stars and Regular polygons).

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 following image illustrates how the Java2D shapes can be used. (Demo application results).


Let's see the shapes in details.

Regular polygon class.

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 image illustrates how it works.

The constructors of RegularPolygon class are following:

    public RegularPolygon(int x, int y, int r, int vertexCount)
    public RegularPolygon(int x, int y, int r, int vertexCount, double startAngle)

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).

Star polygon class.

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 image illustrates how star is defined and works.

The constructors of RegularPolygon class are following:

    public StarPolygon(int x, int y, int r, int innerR, int vertexCount) 
    public StarPolygon(int x, int y, int r, int innerR, int vertexCount, double startAngle) 

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).

Round polygon class.

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:

    public RoundPolygon(Polygon p, int arcWidth) 

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.