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