HirstDots.java
1    package npw;
2    
3    import painter.SPainter;
4    import shapes.SCircle;
5    
6    import javax.swing.*;
7    import java.awt.*;
8    import java.util.Random;
9    import java.util.Scanner;
10   
11   public class HirstDots {
12   
13       private void paintTheImage(){
14           int radius1 = getNumber("circle radius");
15           int radius2 = getNumber("circle radius length");
16           SPainter painter = new SPainter("Hirst Dots", radius1*2+50, radius1*2+50);
17           painter.setBrushWidth(3);
18           SCircle circle1 = new SCircle(radius1);
19           SCircle circle2 = new SCircle(radius2);
20           paintCircleOfCircles(painter, circle1, circle2);
21       }
22   
23       private void paintCircleOfCircles(SPainter painter, SCircle circle1, SCircle circle2){
24           painter.mfd(circle1.radius());
25           painter.tr();
26           double moved = 0;
27           while (moved < circle1.diameter()) {
28               double chord = chordLength(circle1.radius() - moved, circle1);
29               int circles = circlesOnLineCount(chord, circle2.diameter());
30               paintRow(painter, circle2, circles);
31               nextRow(painter, circle2.diameter());
32               moved = moved + circle2.diameter();
33           }
34           painter.tl();
35           painter.mfd(circle1.radius());
36       }
37   
38       private void nextRow(SPainter painter, double rowHeight){
39           painter.tr();
40           painter.mfd(rowHeight);
41           painter.tl();
42       }
43   
44       private void paintRow(SPainter painter, SCircle circle2, int circlesToPaint){
45           double centerOffset = ( (circlesToPaint * circle2.diameter()) / 2) - circle2.diameter()/2;
46           painter.mbk(centerOffset);
47           int painted = 0;
48           while (painted < circlesToPaint){
49               paintOneCircle(painter, circle2);
50               painter.mfd(circle2.diameter());
51               painted = painted + 1;
52           }
53           painter.mbk(centerOffset + circle2.diameter());
54       }
55   
56       private void paintOneCircle(SPainter painter, SCircle circle2){
57           circle2.s2();
58           painter.setColor(randomColor());
59           painter.paint(circle2);
60           circle2.x2();
61       }
62   
63       private static int circlesOnLineCount(double lineLength, double sideLength){
64           int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
65           return circles;
66       }
67   
68       private double chordLength(double yOffset, SCircle circle1){
69           double xLength = Math.sqrt(Math.pow(circle1.radius(), 2) - Math.pow(yOffset, 2));
70           double chordLength = xLength * 2;
71           return chordLength;
72       }
73   
74       private static int getNumber(String prompt) {
75           String nss = JOptionPane.showInputDialog(null,prompt+"?");
76           Scanner scanner = new Scanner(nss);
77           return scanner.nextInt();
78       }
79   
80       private static Color randomColor() {
81           Random rgen = new Random();
82           int r = rgen.nextInt(256);
83           int g = rgen.nextInt(256);
84           int b = rgen.nextInt(256);
85           return new Color(r,g,b);
86       }
87   
88       public HirstDots() {
89           paintTheImage();
90       }
91   
92       public static void main(String[] args) {
93           SwingUtilities.invokeLater(new Runnable() {
94               public void run() {
95                   new HirstDots();
96               }
97           });
98       }
99   }