SimpleDots.java
1    package npw;
2    
3    import painter.SPainter;
4    import shapes.SCircle;
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   
13   
14       private void paintTheImage(){
15           // Get the input information
16           int radius = getNumber("circle radius");
17           int diameter = getNumber("dots diamater");
18   
19           // Establish the painter
20           SPainter painter = new SPainter("Simple Dots", radius*2+50, radius*2+50);
21           painter.setBrushWidth(3);
22           SCircle circle = new SCircle(radius);
23           SCircle dots = new SCircle(diameter/2);
24   
25           // Paint the squares
26           getColor(painter);
27           paintCircleOfDots(painter, circle, dots);
28       }
29   
30       private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dots){
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 squares = squaresOnLineCount(chord, dots.diameter());
39               paintRow(painter, dots, squares);
40               nextRow(painter, dots.diameter());
41               moved = moved + dots.diameter();
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 dots, int squaresToPaint){
57           // Move backward 1/2 of the length we're painting to get ready to paint the row.
58           double centerOffset = ( (squaresToPaint * dots.diameter()) / 2) - dots.diameter()/2;
59           painter.mbk(centerOffset);
60   
61           // Paint the row of squares.
62           int painted = 0;
63           while (painted < squaresToPaint){
64               paintOneSquare(painter, dots);
65               painter.mfd(dots.diameter());
66               painted = painted + 1;
67           }
68   
69           // Make the method invariant with respect to painter position.
70           painter.mbk(centerOffset + dots.diameter());
71       }
72   
73       private void paintOneSquare(SPainter painter, SCircle dots){
74           dots.s2();
75           painter.paint(dots);
76           dots.x2();
77       }
78   
79       private static void getColor(SPainter painter) {
80           while ( true ) {
81               String command = JOptionPane.showInputDialog(null, "Color?");
82               if (command == null) {
83                   command = "exit";
84               } // user clicked on Cancel
85               if (command.equalsIgnoreCase("blue")) {
86                   painter.setColor(Color.blue);
87                   break;
88               } else if (command.equalsIgnoreCase("red")) {
89                   painter.setColor(Color.red);
90                   break;
91               } else if (command.equalsIgnoreCase("green")) {
92                   painter.setColor(Color.green);
93                   break;
94               } else {
95                   painter.setColor(Color.black);
96                   break;
97               }
98           }
99       }
100  
101      private static int squaresOnLineCount(double lineLength, double sideLength){
102          int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
103          return squares;
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  
119  
120      private static Color randomColor() {
121          Random rgen = new Random();
122          int r = rgen.nextInt(256);
123          int g = rgen.nextInt(256);
124          int b = rgen.nextInt(256);
125          return new Color(r,g,b);
126      }
127  
128      public SimpleDots() {
129          paintTheImage();
130      }
131  
132      public static void main(String[] args) {
133          SwingUtilities.invokeLater(new Runnable() {
134              public void run() {
135                  new npw.SimpleDots();
136              }
137          });
138      }
139  }
140