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    
10   import javax.swing.*;
11   import java.awt.*;
12   import java.util.Scanner;
13   
14   public class SimpleDots {
15   
16       private void paintTheImage(){
17           // Get the input information
18           int radius = getNumber("Circle Radius");
19           int radius2 = getNumber("Circles Diameter");
20   
21           // Establish the painter
22           SPainter painter = new SPainter("Circle of Circles", radius*2+50, radius*2+50);
23           painter.setBrushWidth(3);
24           SCircle circle = new SCircle(radius);
25           SCircle circle1 = new SCircle(radius2);
26           setColor(painter);
27   
28   
29   
30           // Paint the squares
31   
32           paintCircleOfSquares(painter, circle1, circle);
33       }
34   
35   
36   
37   
38   
39   
40   
41       private void paintCircleOfSquares(SPainter painter, SCircle circle1, SCircle circle){
42           // Position the painter to begin drawing the rows
43           painter.mfd(circle.radius());
44           painter.tr();
45   
46           // Paint the circle of squares
47           double moved = 0;
48           while (moved < circle.diameter()) {
49               double chord = chordLength(circle.radius() - moved, circle);
50   
51               int squares = squaresOnLineCount(chord, circle.diameter());
52   
53               System.out.println("chord" + chord);
54   
55               paintRow(painter, circle1, squares);
56               nextRow(painter, circle1.diameter());
57               System.out.println("moved " + moved);
58               moved = moved + circle1.diameter();
59           }
60   
61           // Make the method invariant with respect to painter position
62           painter.tl();
63           painter.mfd(circle.radius());
64       }
65   
66       // Move to the next row
67       private void nextRow(SPainter painter, double rowHeight){
68           painter.tr();
69           painter.mfd(rowHeight);
70           painter.tl();
71       }
72   
73       // Assumes the painter is at the center of the row to paint, facing right.////////////////////////////////
74       private void paintRow(SPainter painter, SCircle circle1, int squaresToPaint){
75   
76           // Move backward 1/2 of the length we're painting to get ready to paint the row.
77           double centerOffset = ( (squaresToPaint * circle1.diameter()) / 2) - circle1.diameter()/2;
78           painter.mbk(centerOffset);
79   
80           System.out.println("squarestopaint " + squaresToPaint);
81           System.out.println("COffset " + centerOffset);
82   
83           // Paint the row of squares.
84           int painted = 0;
85           while (painted < squaresToPaint){
86               paintOneSquare(painter, circle1);
87               painter.mfd(circle1.diameter());
88               painted = painted + 1;
89           }
90   
91           // Make the method invariant with respect to painter position.
92           painter.mbk(centerOffset + circle1.diameter());
93       }
94   
95       private void paintOneSquare(SPainter painter, SCircle circle1){
96           circle1.s2();
97           painter.paint(circle1);
98           circle1.x2();
99       }
100  
101      private static int squaresOnLineCount(double lineLength, double sideLength){
102          int squares = ( (int)Math.ceil( ( lineLength / (sideLength*.05) )));
103          System.out.println("lineL" + lineLength);
104          System.out.println("Sqr " + squares);
105          return squares;
106      }
107  
108      private double chordLength(double yOffset, SCircle circle){
109          double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
110          double chordLength = xLength * 2;
111          return chordLength;
112      }
113  
114      private static int getNumber(String prompt) {
115          String nss = JOptionPane.showInputDialog(null,prompt+"?");
116          Scanner scanner = new Scanner(nss);
117          return scanner.nextInt();
118      }
119      private void setColor(SPainter painter){
120          while ( true ) {
121              painter.setColor(Color.BLACK);
122              String command = JOptionPane.showInputDialog(null, "Color?");
123  
124              if (command.equalsIgnoreCase("blue")) {
125                  painter.setColor(Color.BLUE);
126                  break;
127              } else if (command.equalsIgnoreCase("red")) {
128                  painter.setColor(Color.RED);
129                  break;
130              } else if (command.equalsIgnoreCase("green")) {
131                  painter.setColor(Color.GREEN);
132                  break;
133  
134              } else {
135                  JOptionPane.showMessageDialog(null, "Unrecognizable : "
136                          + command.toUpperCase());
137                  painter.setColor(Color.BLACK);
138                  break;
139              }
140          }
141      }
142  
143      public SimpleDots() {
144          paintTheImage();
145      }
146  
147      public static void main(String[] args) {
148          SwingUtilities.invokeLater(new Runnable() {
149              public void run() {
150                  new SimpleDots();
151              }
152          });
153      }
154  }