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