This is a graphical pattern I did as homework for the Computer Graphics class at my university. Running the program gives us the following pattern, with which the user can play around with the slider for making the pattern bigger or smaller. Execute the program by running the main method from PatternTest class.
Rendering was accomplished by using Area and AffineTransform classes from Java 2D package. Most of the work had to do with figuring out the coordinates of the objects which are explained below.
We will call the sticks with circles above them around circles lollipops. We need a method that draws the
- Draw the
$n-th$ circle of the pattern. - Draw a lollipop above the circle.
- Keep rotating the lollipop and drawing it around the circle.
The class which models a lollipop is Lollipop.java and the class that has this algorithm is Pattern.java. The latter class calculates the position of circles and lollipops, such as the radius of the
The concentric circles are all equally distanced from one another with the distance
We can see that the first circle(the one filled with color) has a distance
Plugging
We will call the rectangle of the lollipop a stick and the circle above it a pop (I was so excited to call it pop). We want to draw the lollipop such that the
Clearly the cordinates of point
To give the llollipop the look as above, the coordinate D was shifted down for a few pixels.
The next step is to move this lollipop to the top of the circle. Since
The cordinates of the rised up lollipop are:
The
The angle which one lollipop should be rotoated and be drawn is
AffineTransform r = new AffineTransform();
int n0 = f(n);
r.rotate(2 * Math.PI / n0);
for (int i = 0; i < n0; i++) {
lollipopArea = lollipopArea.createTransformedArea(r);
g2.fill(lollipopArea);
}
It was important rotoating the lollipop and then drawing it, rather then adding all the lollipops together into one Area and then rendering it beacuse this approach had super slow excectution time.
Since our method draws the
for (int i = 0; i < circles; i++) {
Pattern.drawPattern(center_x, center_y, center_diameter, i, g2);
}
It is also possible just to draw for example only the seventh circle of the pattern with the lollipops around it as shown in the image below:
Back to our for loop, if circles parameter equaled three, then we would get the following pattern