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