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