HirstSquares.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 HirstSquares {
13       private void paintTheImage(){
14           // Get the input information
15           int radius = getNumber("circle radius");
16           int side = getNumber("square side length");
17   
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           SSquare square = new SSquare(side);
23   
24           // Paint the squares
25           paintHirstSquares(painter, circle, square);
26       }
27   
28       private void paintHirstSquares(SPainter painter, SCircle circle, SSquare square){
29           // Position the painter to begin drawing the rows
30           painter.mfd(circle.radius());
31           painter.tr();
32           // Paint the circle of squares
33           double moved = 0;
34           while (moved < circle.diameter()) {
35               double chord = chordLength(circle.radius() - moved, circle);
36               int squares = squaresOnLineCount(chord, square.side());
37               paintRow(painter, square, squares);
38               nextRow(painter, square.side());
39               moved = moved + square.side();
40           }
41           // Make the method invariant with respect to painter position
42           painter.tl();
43           painter.mfd(circle.radius());
44       }
45   
46       // Move to the next row
47       private void nextRow(SPainter painter, double rowHeight){
48           painter.tr();
49           painter.mfd(rowHeight);
50           painter.tl();
51       }
52   
53       // Assumes the painter is at the center of the row to paint, facing right.
54       private void paintRow(SPainter painter, SSquare square, int squaresToPaint){
55           // Move backward 1/2 of the length we're painting to get ready to paint the row.
56           double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2;
57           painter.mbk(centerOffset);
58   
59           // Paint the row of squares.
60           int painted = 0;
61           while (painted < squaresToPaint){
62               paintOneSquare(painter, square);
63               painter.mfd(square.side());
64               painted = painted + 1;
65           }
66   
67           // Make the method invariant with respect to painter position.
68           painter.mbk(centerOffset + square.side());
69       }
70   
71       private void paintOneSquare(SPainter painter, SSquare square){
72           square.s2();
73           painter.setColor(randomColor());
74           painter.paint(square);
75           painter.setColor(Color.BLACK);
76           painter.draw(square);
77           square.x2();
78   
79   
80   
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 HirstSquares() {
109          paintTheImage();
110      }
111  
112      public static void main(String[] args) {
113          SwingUtilities.invokeLater(new Runnable() {
114              public void run() {
115                  new HirstSquares();
116              }
117          });
118      }
119  }
120