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