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