HirstSquares.java
1    /* 
2     * A program to paint, centered on the canvas, a circle of randomly colored squares that are spaced apart 
3     */
4    
5    package npw;
6    
7    import painter.SPainter;
8    import shapes.SCircle;
9    import shapes.SSquare;
10   
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           // Get the input information
20           int radius = getNumber("circle radius");
21           int side = getNumber("square side length");
22   
23           // Establish the painter
24           SPainter painter = new SPainter("HirstSquares", radius*2+50, radius*2+50);
25           painter.setBrushWidth(3);
26           SCircle circle = new SCircle(radius);
27           SSquare square = new SSquare(side);
28   
29           // Paint the squares
30           paintCircleOfSquares(painter, circle, square);
31       }
32   
33       private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square){
34           // Position the painter to begin drawing the rows
35           painter.mfd(circle.radius());
36           painter.tr();
37   
38   
39           // Paint the circle of squares
40           double moved = 0;
41           while (moved < circle.diameter()) {
42               double chord = chordLength(circle.radius() - moved, circle);
43               int squares = squaresOnLineCount(chord, square.side());
44               paintRow(painter, square, squares);
45               nextRow(painter, square.side());
46               moved = moved + square.side();
47           }
48           // Make the method invariant with respect to painter position
49           painter.tl();
50           painter.mfd(circle.radius());
51   
52       }
53   
54       // Move to the next row
55       private void nextRow(SPainter painter, double rowHeight){
56           painter.tr();
57           painter.mfd(rowHeight);
58           painter.tl();
59   
60   
61       }
62   
63       // Assumes the painter is at the center of the row to paint, facing right.
64       private void paintRow(SPainter painter, SSquare square, int squaresToPaint){
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   
70           // Paint the row of squares.
71           int painted = 0;
72           while (painted < squaresToPaint){
73               paintOneSquare(painter, square);
74               painter.mfd(square.side());
75               painted = painted + 1;
76   
77           }
78   
79           // Make the method invariant with respect to painter position.
80           painter.mbk(centerOffset + square.side());
81       }
82   
83       private void paintOneSquare(SPainter painter, SSquare square){
84           square.shrink(square.side() * 0.50);
85           painter.setColor(randomColor());
86           painter.paint(square);
87           painter.setColor(Color.BLACK);
88           painter.draw(square);
89           square.expand(square.side()); ///Needed for invariance
90       }
91   
92       private static int squaresOnLineCount(double lineLength, double sideLength){
93           int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
94           return squares;
95       }
96   
97       private double chordLength(double yOffset, SCircle circle){
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          String nss = JOptionPane.showInputDialog(null,prompt+"?");
105          Scanner scanner = new Scanner(nss);
106          return scanner.nextInt();
107      }
108  
109      private static Color randomColor() {
110          Random rgen = new Random();
111          int r = rgen.nextInt(256);
112          int g = rgen.nextInt(256);
113          int b = rgen.nextInt(256);
114          return new Color(r,g,b);
115      }
116  
117      public HirstSquares() {
118          paintTheImage();
119      }
120  
121      public static void main(String[] args) {
122          SwingUtilities.invokeLater(new Runnable() {
123              public void run() {
124                  new HirstSquares();
125              }
126          });
127      }
128  }
129  
130