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