HirstDots.java
1    /* 
2     * A program to paint, centered on the canvas, a circle of randomly colored, 
3     * circles with space in between them. 
4     */
5    
6    package npw;
7    
8    import painter.SPainter;
9    import shapes.SCircle;
10   
11   import javax.swing.*;
12   import java.awt.*;
13   import java.util.Random;
14   import java.util.Scanner;
15   
16   public class HirstDots {
17   
18       private void paintTheImage() {
19           // Get the input information
20           int radius = getNumber("circle radius");
21           int side = getNumber("dot radius");
22   
23           // Establish the painter
24           SPainter painter = new SPainter("Hirst Dots", radius * 2 + 50, radius * 2 + 50);
25           painter.setBrushWidth(3);
26           SCircle circle = new SCircle(radius);
27           SCircle dot = new SCircle(side);
28   
29           // Paint the dots
30           paintHirstDots(painter, circle, dot);
31       }
32   
33       private void paintHirstDots(SPainter painter, SCircle circle, SCircle dot) {
34           // Position the painter to begin drawing the rows
35           painter.mfd(circle.radius());
36           painter.tr();
37   
38           // Paint the circle of dots
39           double moved = 0;
40           while (moved < circle.diameter()) {
41               double chord = chordLength(circle.radius() - moved, circle);
42               int dots = dotsOnLineCount(chord, dot.diameter());
43               paintRow(painter, dot, dots);
44               nextRow(painter, dot.diameter());
45               moved = moved + dot.diameter();
46           }
47   
48           // Make the method invariant with respect to painter position
49           painter.tl();
50           painter.mfd(circle.radius());
51       }
52       // Move to the next row
53       private void nextRow (SPainter painter,double rowHeight) {
54           painter.tr();
55           painter.mfd(rowHeight);
56           painter.tl();
57       }
58   
59       // Assumes the painter is at the center of the row to paint, facing right
60       private void paintRow (SPainter painter, SCircle dot,int dotsToPaint){
61   
62           // Move backward 1/2 of the length we're painting to get ready to paint the row
63           double centerOffset = ((dotsToPaint * dot.diameter()) / 2) - dot.diameter() / 2;
64           painter.mbk(centerOffset);
65   
66           // Paint the row of dots
67           int painted = 0;
68           while (painted < dotsToPaint) {
69               paintOneDot(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 paintOneDot (SPainter painter, SCircle dot){
79   
80           painter.setColor(randomColor());
81           dot.s2();
82           painter.paint(dot);
83           dot.x2();
84   
85       }
86   
87       private static int dotsOnLineCount ( 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  
125  
126  
127  
128