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