SimpleDots.java
1    /* 
2    ** A Program to paint, centered in the canvas, a circle of chosen one specific color of the circle which are 
3    * seperated dots from each other 
4     
5     */
6    
7    package npw;
8    
9    import painter.SPainter;
10   import shapes.SCircle;
11   import shapes.SSquare;
12   
13   import javax.swing.*;
14   import java.awt.*;
15   import java.util.Random;
16   import java.util.Scanner;
17   
18   public class SimpleDots {
19       SimpleDots() {
20           paintTheImage();
21       }
22   
23       public static void main(String[] args) {
24           SwingUtilities.invokeLater(new Runnable() {
25               @Override
26               public void run() { new SimpleDots();
27               }
28           });
29       }
30   
31       private void paintTheImage() {
32           //Get the input information
33           int radius = getNumber("Circle radius");
34           int side = getNumber("square side length");
35           String color= getString("circle color");
36   
37           //Establish the painter
38           SPainter painter = new SPainter("SimpleDots", radius * 2 + 50, radius * 2 + 50);
39           painter.setBrushWidth(0);
40           SCircle circle = new SCircle(radius);
41           SSquare square = new SSquare(side);
42   
43           //paint the squares
44   
45           paintCircleOfSquares(painter, circle, square, color);
46       }
47   
48       private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square, String color) {
49           //position the painter to begin drawing the rows
50           painter.mfd(circle.radius());
51           painter.tr();
52           //paint the circle of squares
53           double moved = 0;
54           while (moved < circle.diameter()) {
55               double chord = chordLength(circle.radius() - moved, circle);
56               int squares = squareOnLineCount(chord, square.side());
57               paintRow(painter, square, squares, color);
58               nextRow(painter, square.side());
59               moved = moved + square.side();
60   
61           }
62           //Make the method invariant with respect to painter position
63           painter.tl();
64           painter.mfd(circle.radius());
65       }
66   
67       // Move to the next row
68       private void nextRow(SPainter painter, double rowHeight) {
69           painter.tr();
70           painter.mfd(rowHeight);
71           painter.tl();
72       }
73   
74       //Assume the painter is at the center of the row to paint, facing right.
75       private void paintRow(SPainter painter, SSquare square, int squaresToPaint, String color) {
76           //move backward 1/2 of the length we're painting to get ready to paint the row.
77           double centerOfset = ((squaresToPaint * square.side()) / 2) - square.side() / 2;
78           painter.mbk(centerOfset);
79   
80           //paint the row of squares.
81           int painted = 0;
82           while (painted < squaresToPaint) {
83               paintOneSquare(painter, square, color);
84               painter.mfd(square.side());
85               painted = painted + 1;
86           }
87   
88           //Make the  method invariant with respect to painter position.Deterministic Invention
89           painter.mbk(centerOfset + square.side());
90       }
91   
92       private void paintOneSquare(SPainter painter, SSquare square, String color) {
93           if (color.equals("red")){
94               painter.setColor(Color.RED);
95           }
96           if (color.equals("blue")){
97               painter.setColor(Color.BLUE);
98           }
99           if (color.equals("green")){
100              painter.setColor(Color.GREEN);
101          }
102  
103          square.s2();
104          SCircle circle = square.inscribingCircle();
105          painter.paint(circle);
106          painter.setColor(Color.black);
107          painter.draw(circle);
108          square.expand(square.side());
109      }
110  
111      private static int squareOnLineCount(double lineLength, double sideLenth) {
112          int squares = ((int) Math.ceil((lineLength - sideLenth) / sideLenth) + 1);
113          return squares;
114      }
115  
116      private double chordLength(double yOffset, SCircle circle) {
117          double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
118          double chordLength = xLength * 2;
119          return chordLength;
120      }
121  
122      private static int getNumber(String prompt) {
123          String nss = JOptionPane.showInputDialog(null, prompt + "?");
124          Scanner scanner = new Scanner(nss);
125          return scanner.nextInt();
126      }
127  
128      private static String getString(String prompt) {
129          String nss = JOptionPane.showInputDialog(null, prompt + "?");
130          Scanner scanner = new Scanner(nss);
131          return scanner.nextLine();
132      }
133      }
134  
135  
136  
137  
138  
139  
140  
141