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