SimpleDots.java
1    /* 
2     a program to paint, centered on the canvas, a circle of dots of a given color, with spaces. 
3     */
4    
5    package npw;
6    
7    import painter.SPainter;
8    import shapes.SCircle;
9    import shapes.SSquare;
10   
11   import javax.swing.*;
12   import java.awt.*;
13   import java.util.Scanner;
14   
15   public class SimpleDots {
16   
17       private void paintTheImage() {
18           // get the input info
19           int radius = getNumber("circle radius");
20           int rad = getNumber("dot radius");
21           // establish painter
22           SPainter painter = new SPainter("SimpleDots", radius*2+50, radius*2+50);
23           changeColor(painter);
24           painter.setBrushWidth(3);
25           SCircle circle = new SCircle(radius);
26           SCircle dot = new SCircle(rad);
27           // paint the dots
28           paintCircleOfDots(painter, circle, dot);
29       }
30   
31       private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot) {
32           // position the painter to begin drawing the rows
33           painter.mfd(circle.radius());
34           painter.tr();
35           // paint the circle of dots
36           double moved = 0;
37           while (moved < circle.diameter()) {
38               double chord = chordLength(circle.radius()-moved, circle);
39               int dots = dotsOnLineCount(chord, dot.diameter());
40               paintRow(painter, dot, dots);
41               nextRow(painter, dot.diameter());
42               moved = moved + dot.diameter();
43           }
44           // make the method invariant with respect to painter position
45           painter.tl();
46           painter.mfd(circle.radius());
47       }
48   
49       // move to the next row
50       private void nextRow (SPainter painter, double rowHeight) {
51           painter.tr();
52           painter.mfd(rowHeight);
53           painter.tl();
54       }
55   
56       // assumes the painter is at the center of the row to paint, facing right.
57       private void paintRow (SPainter painter, SCircle dot, int dotsToPaint) {
58           // move backward one half of the length we're painting to get ready to paint the row
59           double centerOffset = ((dotsToPaint*dot.diameter())/2)- dot.diameter()/2;
60           painter.mbk(centerOffset);
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           // make the method invariant with respect to painter position
69           painter.mbk(centerOffset + dot.diameter());
70       }
71   
72       private void paintOneDot(SPainter painter, SCircle dot) {
73           dot.shrink(10);
74           painter.paint(dot);
75           dot.expand(10);
76       }
77   
78       private static int dotsOnLineCount(double lineLength, double sideLength) {
79           int dots = ((int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
80           return dots;
81       }
82   
83       private double chordLength(double yOffset, SCircle circle) {
84           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
85           double chordLength = xLength*2;
86           return chordLength;
87       }
88   
89       private static int getNumber(String prompt) {
90           String nss = JOptionPane.showInputDialog(null,prompt+"?");
91           Scanner scanner = new Scanner(nss);
92           return scanner.nextInt();
93       }
94   
95       private void changeColor(SPainter painter) {
96           String choose = JOptionPane.showInputDialog(null,"red, blue, or green?");
97           Scanner scanner = new Scanner(choose);
98           if (choose.equalsIgnoreCase("blue")) {
99               painter.setColor(Color.blue);
100          } else if (choose.equalsIgnoreCase("red")) {
101              painter.setColor(Color.red);
102          } else if (choose.equalsIgnoreCase("green")) {
103              painter.setColor(Color.green);
104          } else {
105              painter.setColor(Color.black);
106          }
107      }
108  
109      public SimpleDots() {
110          paintTheImage();
111      }
112  
113      public static void main(String[] args) {
114          SwingUtilities.invokeLater(new Runnable() {
115              public void run() {
116                  new SimpleDots();
117              }
118          });
119      }
120  }