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