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