OR, you could use Java's handy dandy polygon class.
I have some source code that was made to draw a flag for a lab, on which I had to draw stars. Stars are a bit more comlex, obviously, but the principle is the same. Also, the code is optimized to make a five-point star, you'll have to figure a way to change that.
The values of 54 and 18 were values away from 90/270 and 0/180 degrees, for each point. You'll need to come up with a way to generate those values for each situation.
Code: Select all
public void drawStar(Graphics graph, int posX, int posY, int outerR, int innerR, int r, int g, int b)
{
int [] x = new int [10];
int [] y = new int [10];
x[0] = (int) posX;
x[1] = (int) Math.round(posX+Math.cos(54 * (Math.PI/180))*(innerR));
x[2] = (int) Math.round(posX+Math.cos(18 * (Math.PI/180))*(outerR));
x[3] = (int) Math.round(posX+Math.cos(18 * (Math.PI/180))*(innerR));
x[4] = (int) Math.round(posX+Math.cos(54 * (Math.PI/180))*(outerR));
x[5] = (int) posX;
x[6] = (int) Math.round(posX-Math.cos(54 * (Math.PI/180))*(outerR));
x[7] = (int) Math.round(posX-Math.cos(18 * (Math.PI/180))*(innerR));
x[8] = (int) Math.round(posX-Math.cos(18 * (Math.PI/180))*(outerR));
x[9] = (int) Math.round(posX-Math.cos(54 * (Math.PI/180))*(innerR));
y[0] = (int) posY-outerR;
y[1] = (int) Math.round(posY-Math.sin(54 * (Math.PI/180))*(innerR));
y[2] = (int) Math.round(posY-Math.sin(18 * (Math.PI/180))*(outerR));
y[3] = (int) Math.round(posY+Math.sin(18 * (Math.PI/180))*(innerR));
y[4] = (int) Math.round(posY+Math.sin(54 * (Math.PI/180))*(outerR));
y[5] = (int) posY+innerR;
y[6] = (int) Math.round(posY+Math.sin(54 * (Math.PI/180))*(outerR));
y[7] = (int) Math.round(posY+Math.sin(18 * (Math.PI/180))*(innerR));
y[8] = (int) Math.round(posY-Math.sin(18 * (Math.PI/180))*(outerR));
y[9] = (int) Math.round(posY-Math.sin(54 * (Math.PI/180))*(innerR));
Color myColor = new Color(r,g,b);
graph.setColor(myColor);
Polygon star = new Polygon();
for (int count=0; count<10; count++)
{
star.addPoint(x[count], y[count]);
}
graph.fillPolygon(star);
/*graph.drawLine(posX, posY, posX, posY);
graph.drawLine(x[0], y[0], x[0], y[0]);
graph.drawLine(x[2], y[2], x[2], y[2]);
graph.drawLine(x[4], y[4], x[4], y[4]);
graph.drawLine(x[6], y[6], x[6], y[6]);
graph.drawLine(x[8], y[8], x[8], y[8]);
graph.drawLine(x[1], y[1]);
graph.drawLine(x[3], y[3]);
graph.drawline(x[5], y[5]);
graph.drawLine(x[7], y[7]);
graph.drawLine(x[9], y[9]);*/
}