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