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