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