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   
14       private void paintTheImage(){
15           // Get the input information
16           int radius = getNumber("circle radius");
17           int side = getNumber("square side length");
18   
19           // Establish the painter
20           SPainter painter = new SPainter("Hirst Squares", radius*2+50, radius*2+50);
21           painter.setBrushWidth(3);
22           SCircle circle = new SCircle(radius);
23           SSquare square = new SSquare(side);
24   
25           // Paint the squares
26           paintCircleOfSquares(painter, circle, square);
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           // Make the method invariant with respect to painter position
43           painter.tl();
44           painter.mfd(circle.radius());
45       }
46   
47       // Move to the next row
48       private void nextRow(SPainter painter, double rowHeight){
49           painter.tr();
50           painter.mfd(rowHeight);
51           painter.tl();
52       }
53   
54       // Assumes the painter is at the center of the row to paint, facing right.
55       private void paintRow(SPainter painter, SSquare square, int squaresToPaint){
56           // Move backward 1/2 of the length we're painting to get ready to paint the row.
57           double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2;
58           painter.mbk(centerOffset);
59   
60           // Paint the row of squares.
61           int painted = 0;
62           while (painted < squaresToPaint){
63               paintOneSquare(painter, square);
64               painter.mfd(square.side());
65               painted = painted + 1;
66           }
67   
68           // Make the method invariant with respect to painter position.
69           painter.mbk(centerOffset + square.side());
70       }
71   
72       private void paintOneSquare(SPainter painter, SSquare square){
73           square.s2();
74           painter.setColor(randomColor());
75           painter.paint(square);
76           painter.setColor(Color.BLACK);
77           painter.draw(square);
78           square.x2();
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