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