SimpleDots.java
1    
2    /* 
3     * A program to paint, centered on the canvas, a circle of circles where the color is chosen by the user 
4     */
5    
6    package npw;
7    
8    import painter.SPainter;
9    import shapes.SCircle;
10   
11   import javax.swing.*;
12   import java.awt.*;
13   import java.util.Scanner;
14   
15   public class SimpleDots {
16   
17       private void paintTheImage(){
18           // Get the input information
19           int radius = getNumber("circle radius");
20           int circlesRadius = getNumber("circles radius");
21           String dotsColor = getColor("Color of dots? (red, blue, or green)");
22           dotsColor = dotsColor.toLowerCase();
23   
24           // Establish the painter
25           SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50);
26           painter.setBrushWidth(3);
27           SCircle circle = new SCircle(radius);
28           SCircle circles = new SCircle(circlesRadius);
29   
30           // Paint the squares
31           paintCircleOfSquares(painter, circle, circles, dotsColor);
32       }
33   
34   
35       private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle circles, String dotsColor){
36           // Position the painter to begin drawing the rows
37           painter.mfd(circle.radius());
38           painter.tr();
39           // Paint the circle of squares
40           double moved = 0;
41           while (moved < circle.diameter()) {
42               double chord = chordLength(circle.radius() - moved, circle);
43               int squares = squaresOnLineCount(chord, circles.diameter());
44               paintRow(painter, circles, squares, dotsColor);
45               nextRow(painter, circles.diameter());
46               moved = moved + circles.diameter();
47           }
48           // Make the method invariant with respect to painter position
49           painter.tl();
50           painter.mfd(circle.radius());
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       // Assumes the painter is at the center of the row to paint, facing right.
61       private void paintRow(SPainter painter, SCircle circles, int squaresToPaint, String dotsColor){
62           // Move backward 1/2 of the length we're painting to get ready to paint the row.
63           double centerOffset = ( (squaresToPaint * circles.diameter()) / 2) - circles.diameter()/2;
64           painter.mbk(centerOffset);
65   
66           // Paint the row of squares.
67           int painted = 0;
68           while (painted < squaresToPaint){
69               paintOneSquare(painter, circles, dotsColor);
70               painter.mfd(circles.diameter());
71               painted = painted + 1;
72           }
73   
74           // Make the method invariant with respect to painter position.
75           painter.mbk(centerOffset + circles.diameter());
76       }
77   
78       private void paintOneSquare(SPainter painter, SCircle circles, String dotsColor){
79           chooseColor(dotsColor, painter);        //Makes painter either red, blue, green or black based on user input
80           circles.s2();
81           painter.paint(circles);
82           circles.x2();
83   
84       }
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       private double chordLength(double yOffset, SCircle circle){
93           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
94           double chordLength = xLength * 2;
95           return chordLength;
96       }
97   
98       private static int getNumber(String prompt) {
99           String nss = JOptionPane.showInputDialog(null,prompt + "?");
100          Scanner scanner = new Scanner(nss);
101          return scanner.nextInt();
102      }
103  
104      //Reads a string inputted from user for the color of the dots
105      private String getColor(String prompt) {
106          String in = JOptionPane.showInputDialog(null, prompt);
107          Scanner scanner = new Scanner(in);
108          return scanner.next();
109      }
110  
111      //Chooses the color of dots based on user input (red, blue, green and black only colors accepted)
112      private void chooseColor(String dotsColor, SPainter painter) {
113          if (dotsColor.equals("red")){
114              painter.setColor(Color.RED);
115          }
116          else if (dotsColor.equals("blue")) {
117              painter.setColor(Color.BLUE);
118          }
119          else if(dotsColor.equals("green")) {
120              painter.setColor(Color.GREEN);
121          }
122          else {
123              painter.setColor(Color.BLACK);
124          }
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