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