double angle = 2*Math.PI / holeCount;
Ich denke mit Abstand der Löcher ist der Abstand von Loch Rand zu Loch Rand gemeint. Es ergibt sich dann auch ein harmonischeres Muster.
Code von
@mihe7 etwas nachbearbeitet.
[CODE lang="java" title="Mihes code" highlight="79-80"]import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Test {
private final static int WIDTH = 800;
private final static int HEIGHT = 600;
private Canvas canvas = new Canvas();
@SuppressWarnings("serial")
private static class Canvas extends JComponent {
private List<Shape> shapes = new ArrayList<>();
public void add(Shape shape) {
shapes.add(shape);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D g2 = (Graphics2D) g;
for (Shape shape : shapes)
g2.draw(shape);
}
}
public void run() {
addCircles(15, 5 * 15, 3.85);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(canvas);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
}
/**
* @param holeDiam
* Lochdruchmesser
* @param holeDist
* Abstand der Löcher am Kreis
* @param circleDist
* Abstand der Kreise
*/
private void addCircles(double holeDiam, double holeDist, double circleDist) {
double x = WIDTH / 2;
double y = HEIGHT / 2 - holeDiam;
canvas.add(getCircle(x, y, holeDiam));// add center hole
for (double diam = circleDist * holeDiam; diam < HEIGHT - 2 * holeDiam; diam += circleDist * holeDiam)
addCircleRing(x, y, diam, holeDiam, holeDist); // add circle with holes
}
private void addCircleRing(double x, double y, double diam, double holeDiam, double distance) {
canvas.add(getCircle(x, y, diam)); // add current ring circle
double radius = diam / 2.0;
double holeCount = getHoleCount(diam, holeDiam, distance);
double angle = 2 * Math.PI / holeCount;
for (int i = 0; i < holeCount; i++) {
double cx = x + Math.cos(i * angle) * radius;
double cy = y + Math.sin(i * angle) * radius;
canvas.add(getCircle(cx, cy, holeDiam)); // ring's holes
}
}
private static Shape getCircle(double x, double y, double diam) {
return new Arc2D.Double(x - diam / 2, y - diam / 2, diam, diam, 0, 360, Arc2D.OPEN);
}
private static int getHoleCount(double diam, double holeDiam, double distance) {
double circum = Math.PI * diam;
return (int) (circum / (holeDiam + distance));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().run());
}
}[/CODE]