SimpleDots.java
1    /* 
2     * A program to paint, centered on the canvas, a circle of randomly colored, circles. 
3     */
4    
5    package npw;
6    
7    import painter.SPainter;
8    import shapes.SCircle;
9    
10   import java.util.Scanner;
11   import java.awt.Color;
12   import javax.swing.JOptionPane;
13   import javax.swing.SwingUtilities;
14   
15   
16   public class SimpleDots {
17   
18   
19   
20       private void paintTheImage() {
21           // Get the input information
22           int radius = getNumber("circle radius");
23           int radius1 = getNumber("dot radius");
24   
25   
26   
27           // Establish the painter
28           SPainter painter = new SPainter("Circle of Dots", radius * 2 + 50, radius * 2 + 50);
29           painter.setBrushWidth(3);
30           SCircle circle = new SCircle(radius);
31           SCircle dot = new SCircle(radius1);
32   
33           String command = JOptionPane.showInputDialog(null, "what color" + "?");
34   
35           if (command.equalsIgnoreCase("blue")) {
36               painter.setColor(Color.BLUE);
37   
38           } else if (command.equalsIgnoreCase("red")) {
39               painter.setColor(Color.RED);
40   
41           } else if (command.equalsIgnoreCase("green")) {
42               painter.setColor(Color.GREEN);
43   
44           }
45           else  {
46   
47               painter.setColor(Color.BLACK);
48   
49           }
50   
51           // Paint the squares
52           paintCircleOfDots(painter, circle, dot);
53       }
54   
55   
56   
57   
58       private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot) {
59           // Position the painter to begin drawing the rows
60           painter.mfd(circle.radius());
61           painter.tr();
62           // Paint the circle of dots
63           double moved = 0;
64           while (moved < circle.diameter()) {
65               double chord = chordLength(circle.radius() - moved, circle);
66               int dots = dotsOnLineCount(chord, dot.radius());
67               paintRow(painter, dot, dots);
68               nextRow(painter, dot.radius());
69               moved = moved + dot.radius();
70           }
71           // Make the method invariant with respect to painter position
72           painter.tl();
73           painter.mfd(circle.radius());
74       }
75   
76       // Move to the next row
77       private void nextRow(SPainter painter, double rowHeight) {
78           painter.tr();
79           painter.mfd(rowHeight);
80           painter.tl();
81       }
82   
83       // Assumes the painter is at the center of the row to paint, facing right.
84       private void paintRow(SPainter painter, SCircle dot, int dotsToPaint) {
85           // Move backward 1/2 of the length we're painting to get ready to paint the row.
86           double centerOffset = ((dotsToPaint * dot.radius()) / 2) - dot.radius() / 2;
87           painter.mbk(centerOffset);
88   
89           // Paint the row of dots.
90           int painted = 0;
91           while (painted < dotsToPaint) {
92               paintOneDot(painter, dot);
93               painter.mfd(dot.radius());
94               painted = painted + 1;
95           }
96   
97           // Make the method invariant with respect to painter position.
98           painter.mbk(centerOffset + dot.radius());
99   
100      }
101      private void paintOneDot(SPainter painter, SCircle dot) {
102  
103          dot.s3();
104  
105          painter.paint(dot);
106  
107          dot.x3();
108          }
109  
110  
111  
112      private static int dotsOnLineCount(double lineLength, double sideLength) {
113          int dots = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1);
114          return dots;
115      }
116  
117      private double chordLength(double yOffset, SCircle circle) {
118          double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
119          double chordLength = xLength * 2;
120          return chordLength;
121      }
122  
123      private static int getNumber(String prompt) {
124          String nss = JOptionPane.showInputDialog(null, prompt + "?");
125          Scanner scanner = new Scanner(nss);
126          return scanner.nextInt();
127      }
128  
129      public SimpleDots() {
130          paintTheImage();
131      }
132  
133      public static void main(String[] args) {
134          SwingUtilities.invokeLater(new Runnable() {
135              public void run() {
136                  new SimpleDots();
137              }
138          });
139      }
140  }