CirclesOfSquares.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 CirclesOfSquares {
17   
18       private void paintTheImage() {
19   
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("Circle of 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               paintCircleOfSquares(painter, circle, square);
32       }
33   
34       private void paintCircleOfSquares(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 circles 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           //Make the method invariant with respect to painter position
49               painter.tl();
50               painter.mfd(circle.radius());
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 were painting to get ready to pain the row
62           double centerOffset = ((squaresToPaint * square.side()) / 2) - square.side() / 2;
63           painter.mbk(centerOffset);
64   
65   
66           //Paint the row of squares
67           int painted = 0;
68           while (painted < squaresToPaint) {
69               paintOneSquare(painter, square);
70               painter.mfd(square.side());
71               painted = painted + 1;
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 Color randomColor() {
85           Random rgen = new Random();
86           int r = rgen.nextInt(256);
87           int g = rgen.nextInt(256);
88           int b = rgen.nextInt(256);
89           return new Color(r, g, b);
90       }
91   
92       private int squaresOnLineCount(double lineLength, double sideLength) {
93           int squares = ((int)Math.ceil((lineLength - sideLength) / sideLength ) + 1);
94           return squares;
95       }
96   
97       private double chordLength(double yOffset, SCircle circle) {
98           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
99           double chordLength = xLength * 2;
100          return chordLength;
101  
102      }
103  
104      private int getNumber(String prompt) {
105          String nss = JOptionPane.showInputDialog(null,prompt + "?");
106          Scanner scanner = new Scanner(nss);
107          return scanner.nextInt();
108  
109      }
110  
111      public CirclesOfSquares() {
112          paintTheImage();
113      }
114  
115      public static void main(String [] args) {
116          SwingUtilities.invokeLater(new Runnable() {
117              @Override
118              public void run() {
119                  new CirclesOfSquares();
120              }
121          });
122      }
123  }
124