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   public class HirstDots {
13   
14       private void paintTheImage(){
15           // Get the input information
16           int radius = getNumber("circle radius");
17           int rad = getNumber("square side length");
18   
19           // Establish the painter
20           SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50);
21           painter.setBrushWidth(3);
22           SCircle circle = new SCircle(radius);
23           SCircle dots = new SCircle(rad);
24   
25           // Paint the squares
26           paintCircleOfSquares(painter, circle, dots);
27       }
28   
29       private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle dots){
30           // Position the painter to begin drawing the rows
31           painter.mfd(circle.radius());
32           painter.tr();
33           // Paint the circle of squares
34           double moved = 0;
35           while (moved < circle.diameter()) {
36               double chord = chordLength(circle.radius() - moved, circle);
37               int squares = squaresOnLineCount(chord, dots.radius());
38               paintRow(painter, dots, squares);
39               nextRow(painter, dots.radius());
40               moved = moved + dots.radius();
41   
42           }
43           // Make the method invariant with respect to painter position
44           painter.tl();
45           painter.mfd(circle.radius());
46       }
47   
48       // Move to the next row
49       private void nextRow(SPainter painter, double rowHeight){
50           painter.tr();
51           painter.mfd(rowHeight);
52           painter.tl();
53       }
54   
55       // Assumes the painter is at the center of the row to paint, facing right.
56       private void paintRow(SPainter painter, SCircle dots, int squaresToPaint){
57           // Move backward 1/2 of the length we're painting to get ready to paint the row.
58           double centerOffset = ( (squaresToPaint * dots.radius()) / 2) - dots.radius()/2;
59           painter.mbk(centerOffset);
60   
61           // Paint the row of squares.
62           int painted = 0;
63           while (painted < squaresToPaint){
64               paintOneSquare(painter, dots);
65               painter.mfd(dots.radius());
66               painted = painted + 1;
67           }
68   
69           // Make the method invariant with respect to painter position.
70           painter.mbk(centerOffset + dots.radius());
71       }
72   
73       private void paintOneSquare(SPainter painter, SCircle dots){
74           dots.s5();
75           painter.setColor(randomColor());
76           painter.paint(dots);
77   
78   
79   
80           dots.x5();
81       }
82   
83       private static int squaresOnLineCount(double lineLength, double sideLength){
84           int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
85           return squares;
86       }
87   
88       private double chordLength(double yOffset, SCircle circle){
89           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
90           double chordLength = xLength * 2;
91           return chordLength;
92       }
93   
94       private static int getNumber(String prompt) {
95           String nss = JOptionPane.showInputDialog(null,prompt+"?");
96           Scanner scanner = new Scanner(nss);
97           return scanner.nextInt();
98       }
99   
100      private static Color randomColor() {
101          Random rgen = new Random();
102          int r = rgen.nextInt(256);
103          int g = rgen.nextInt(256);
104          int b = rgen.nextInt(256);
105          return new Color(r,g,b);
106      }
107  
108      public HirstDots() {
109          paintTheImage();
110      }
111  
112      public static void main(String[] args) {
113          SwingUtilities.invokeLater(new Runnable() {
114              public void run() {
115                  new HirstDots();
116              }
117          });
118      }
119  }
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133