SimpleDots.java
1    /* 
2     * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 
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   
20           // Get the input information
21           int rad = getNumber("circle radius");
22           int side = (getNumber("small circle radius")) * 2;
23           int ColorNumber = getNumber("(1 = Red | 2 = Blue | 3 = Green) Color");
24   
25           // Establish the painter
26           SPainter painter = new SPainter("Simple Dots", rad*2+50, rad*2+50);
27           painter.setBrushWidth(3);
28           SCircle circle = new SCircle(rad);
29           SSquare square = new SSquare(side);
30   
31           // Set the painter color
32           SetPainterColor(ColorNumber, painter);
33   
34           // Paint the squares
35           paintCircleOfSquares(painter, circle, square);
36       }
37   
38       private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square){
39   
40           // Position the painter to begin drawing the rows
41           painter.mfd(circle.radius());
42           painter.tr();
43   
44           // Paint the circle of squares
45           double moved = 0;
46           while (moved < circle.diameter()) {
47               double chord = chordLength(circle.radius() - moved, circle);
48               int squares = squaresOnLineCount(chord, square.side());
49               paintRow(painter, square, squares);
50               nextRow(painter, square.side());
51               moved = moved + square.side();
52           }
53   
54           // Make the method invariant with respect to painter position
55           painter.tl();
56           painter.mfd(circle.radius());
57       }
58   
59       // Move to the next row
60       private void nextRow(SPainter painter, double rowHeight){
61           painter.tr();
62           painter.mfd(rowHeight);
63           painter.tl();
64       }
65   
66       // Assumes the painter is at the center of the row to paint, facing right.
67       private void paintRow(SPainter painter, SSquare square, int squaresToPaint){
68   
69           // Move backward 1/2 of the length we're painting to get ready to paint the row.
70           double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2;
71           painter.mbk(centerOffset);
72   
73           // Paint the row of squares.
74           int paint = 0;
75           while (paint < squaresToPaint){
76               paintOneSquare(painter, square);
77               painter.mfd(square.side());
78               paint = paint + 1;
79           }
80   
81           // Make the method invariant with respect to painter position.
82           painter.mbk(centerOffset + square.side());
83       }
84   
85       private void paintOneSquare(SPainter painter, SSquare square){
86           square.s2();
87           painter.paint(square.inscribingCircle());
88           square.x2();
89       }
90   
91       private static int squaresOnLineCount(double lineLength, double sideLength){
92           int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
93           return squares;
94       }
95   
96       private double chordLength(double yOffset, SCircle circle){
97           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
98           double chordLength = xLength * 2;
99           return chordLength;
100      }
101  
102      private static int getNumber(String prompt) {
103          String nss = JOptionPane.showInputDialog(null,prompt+"?");
104          Scanner scanner = new Scanner(nss);
105          return scanner.nextInt();
106      }
107  
108      private static Color randomColor() {
109          Random rgen = new Random();
110          int r = rgen.nextInt(256);
111          int g = rgen.nextInt(256);
112          int b = rgen.nextInt(256);
113          return new Color(r,g,b);
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{
124              painter.setColor(Color.BLACK);
125          }
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 SimpleDots();
136              }
137          });
138      }
139  }
140  
141  
142