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       private void paintTheImage(){
13           // Get the input information
14           int radius = getNumber("circle radius");
15           int rad = getNumber("dot radius length");
16           SPainter painter = new SPainter("Circle of Dots", radius*2+50, radius*2+50);
17           painter.setBrushWidth(3);
18           SCircle circle = new SCircle(radius);
19           SCircle dot = new SCircle(rad);
20           while (true) {
21               String command = JOptionPane.showInputDialog(null,"Command?");
22               if (command==null) { command = "exit"; } //user clicked on cancel
23               if (command.equalsIgnoreCase("blue") ) {
24                   painter.setColor(Color.blue);
25                   paintSimpleDots(painter, circle, dot);
26               } else if (command.equalsIgnoreCase("red")) {
27                   painter.setColor(Color.red);
28                   paintSimpleDots(painter, circle, dot);
29               } else if (command.equalsIgnoreCase("green")) {
30                   painter.setColor(Color.green);
31                   paintSimpleDots(painter, circle, dot);
32               } else if (command.equalsIgnoreCase("black")) {
33                   painter.setColor(Color.black);
34                   paintSimpleDots(painter, circle, dot);
35               } else if (command.equalsIgnoreCase("exit")) {
36   
37                   break;
38   
39               } else {
40                   JOptionPane.showMessageDialog(null, "Unrecognizable command: " + command.toUpperCase());
41   
42               }
43           }
44   
45           // Establish the painter
46   
47   
48   
49           // Paint the squares
50           paintSimpleDots(painter, circle, dot);
51       }
52   
53       private void paintSimpleDots(SPainter painter, SCircle circle, SCircle dot){
54           // Position the painter to begin drawing the rows
55           painter.mfd(circle.radius());
56           painter.tr();
57           // Paint the circle of squares
58           double moved = 0;
59           while (moved < circle.diameter()) {
60               double chord = chordLength(circle.radius() - moved, circle);
61               int dots = dotsOnLineCount(chord, dot.diameter());
62               paintRow(painter, dot, dots);
63               nextRow(painter, dot.diameter());
64               moved = moved + dot.diameter();
65           }
66           // Make the method invariant with respect to painter position
67           painter.tl();
68           painter.mfd(circle.radius());
69       }
70   
71       // Move to the next row
72       private void nextRow(SPainter painter, double rowHeight){
73           painter.tr();
74           painter.mfd(rowHeight);
75           painter.tl();
76       }
77   
78       // Assumes the painter is at the center of the row to paint, facing right.
79       private void paintRow(SPainter painter, SCircle dot, int dotsToPaint){
80           // Move backward 1/2 of the length we're painting to get ready to paint the row.
81           double centerOffset = ( (dotsToPaint * dot.diameter()) / 2) - dot.diameter()/2;
82           painter.mbk(centerOffset);
83   
84           // Paint the row of squares.
85           int painted = 0;
86           while (painted < dotsToPaint){
87               paintOnedot(painter, dot);
88               painter.mfd(dot.diameter());
89               painted = painted + 1;
90           }
91   
92           // Make the method invariant with respect to painter position.
93           painter.mbk(centerOffset + dot.diameter());
94       }
95   
96       private void paintOnedot(SPainter painter, SCircle dot){
97           dot.s2();
98           painter.paint(dot);
99           dot.x2();
100  
101  
102  
103      }
104  
105      private static int dotsOnLineCount(double lineLength, double diameter){
106          int dots = ( (int)Math.ceil( ( lineLength - diameter ) / diameter ) + 1);
107          return dots;
108      }
109  
110      private double chordLength(double yOffset, SCircle circle){
111          double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
112          double chordLength = xLength * 2;
113          return chordLength;
114      }
115  
116      private static int getNumber(String prompt) {
117          String nss = JOptionPane.showInputDialog(null,prompt+"?");
118          Scanner scanner = new Scanner(nss);
119          return scanner.nextInt();
120      }
121  
122      private static Color randomColor() {
123          Random rgen = new Random();
124          int r = rgen.nextInt(256);
125          int g = rgen.nextInt(256);
126          int b = rgen.nextInt(256);
127          return new Color(r,g,b);
128      }
129  
130      public SimpleDots() {
131          paintTheImage();
132      }
133  
134      public static void main(String[] args) {
135          SwingUtilities.invokeLater(new Runnable() {
136              public void run() {
137                  new SimpleDots();
138              }
139          });
140      }
141  }
142