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