HirstDots.java
1    package npw;
2    
3    import painter.SPainter;
4    import shapes.SCircle;
5    import shapes.SSquare;
6    
7    import javax.swing.*;
8    import java.awt.*;
9    import java.util.Random;
10   import java.util.Scanner;
11   
12   public class HirstDots {
13       private void paintTheImage(){
14           // Get the input information
15           int radius = getNumber("circle radius");
16           int rad = getNumber("dot radius length");
17           SPainter painter = new SPainter("Circle of Dots", radius*2+50, radius*2+50);
18           painter.setBrushWidth(3);
19           SCircle circle = new SCircle(radius);
20           SCircle dot = new SCircle(rad);
21   
22           // Establish the painter
23   
24   
25   
26           // Paint the squares
27           paintHirstDots(painter, circle, dot);
28       }
29   
30       private void paintHirstDots(SPainter painter, SCircle circle, SCircle dot){
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 dots = dotsOnLineCount(chord, dot.diameter());
39               paintRow(painter, dot, dots);
40               nextRow(painter, dot.diameter());
41               moved = moved + dot.diameter();
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, SCircle dot, int dotsToPaint){
57           // Move backward 1/2 of the length we're painting to get ready to paint the row.
58           double centerOffset = ( (dotsToPaint * dot.diameter()) / 2) - dot.diameter()/2;
59           painter.mbk(centerOffset);
60   
61           // Paint the row of squares.
62           int painted = 0;
63           while (painted < dotsToPaint){
64               paintOnedot(painter, dot);
65               painter.mfd(dot.diameter());
66               painted = painted + 1;
67           }
68   
69           // Make the method invariant with respect to painter position.
70           painter.mbk(centerOffset + dot.diameter());
71       }
72   
73       private void paintOnedot(SPainter painter, SCircle dot){
74           dot.s2();
75           painter.setColor(randomColor());
76           painter.paint(dot);
77           dot.x2();
78   
79   
80   
81       }
82   
83       private static int dotsOnLineCount(double lineLength, double diameter){
84           int dots = ( (int)Math.ceil( ( lineLength - diameter ) / diameter ) + 1);
85           return dots;
86       }
87   
88       private double chordLength(double yOffset, SCircle circle){
89           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
90           double chordLength = xLength * 2;
91           return chordLength;
92       }
93   
94       private static int getNumber(String prompt) {
95           String nss = JOptionPane.showInputDialog(null,prompt+"?");
96           Scanner scanner = new Scanner(nss);
97           return scanner.nextInt();
98       }
99   
100      private static Color randomColor() {
101          Random rgen = new Random();
102          int r = rgen.nextInt(256);
103          int g = rgen.nextInt(256);
104          int b = rgen.nextInt(256);
105          return new Color(r,g,b);
106      }
107  
108      public HirstDots() {
109          paintTheImage();
110      }
111  
112      public static void main(String[] args) {
113          SwingUtilities.invokeLater(new Runnable() {
114              public void run() {
115                  new HirstDots();
116              }
117          });
118      }
119  }
120