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