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