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