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