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