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