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