CircleOfSquares.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 CircleOfSquares {
17   
18       private void paintTheImage(){
19           // Get the input information
20           int rad = getNumber("circle radius");
21           int side = getNumber("square side length");
22   
23           // Establish the painter
24           SPainter painter = new SPainter("Circle of Squares", rad*2+50, rad*2+50);
25           painter.setBrushWidth(3);
26           SCircle circle = new SCircle(rad);
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           // 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 paint = 0;
67           while (paint < squaresToPaint){
68               paintOneSquare(painter, square);
69               painter.mfd(square.side());
70               paint = paint + 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           painter.setColor(randomColor());
79           painter.paint(square);
80           painter.setColor(Color.BLACK);
81           painter.draw(square);
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 CircleOfSquares() {
110          paintTheImage();
111      }
112  
113      public static void main(String[] args) {
114          SwingUtilities.invokeLater(new Runnable() {
115              public void run() {
116                  new CircleOfSquares();
117              }
118          });
119      }
120  }