HirstDots.java
1    /* 
2     * Assignment 4 Program 4: Hirst Circles in a Circle. 
3     * A program to paint, centered on the canvas, a circle of smaller circles with Hirst 
4     */
5    
6    package npw;
7    
8    import painter.SPainter;
9    import shapes.SCircle;
10   import shapes.SSquare;
11   import javax.swing.*;
12   import java.awt.*;
13   import java.util.Random;
14   import java.util.Scanner;
15   
16   
17   public class HirstDots {
18       public static void main(String[] args){
19           SwingUtilities.invokeLater(new Runnable() {
20               public void run() {
21                   new HirstDots();
22               }
23           });
24       }
25       public HirstDots() {
26           paintTheImage();
27       }
28   
29       private void paintTheImage() {
30           // Get the input information
31           // create new method named "getNumber" to scan from input
32           int radius = getNumber("circle radius");
33           int side = getNumber("square side length");
34   
35   
36           // Establish the painter
37           // Create new painter with Canvas names "Circle of Squares" and side of Circle diameter plus 50
38           SPainter painter = new SPainter("Circle of Hirst Dots", radius*2+50, radius*2+50);
39           // Painter's brushWidth widen so that the side of the square could be more visible
40           painter.setBrushWidth(3);
41           SCircle circle = new SCircle(radius);
42           SSquare square = new SSquare(side);
43   
44           // Paint the squares
45           // Created new method named "paintCircleOfSquares" with parameter of painter,circle,and square
46           paintCircleOfSquares(painter, circle, square);
47       }
48   
49       private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square) {
50           // Position the painter to begin drawing the rows
51           painter.mfd(circle.radius()); // painter move forward as circle radius
52           painter.tr();
53           // Paint the circle of squares
54           double moved = 0;
55           while (moved < circle.diameter()){
56               double chord = chordLength(circle.radius()-moved,circle);
57               // number of square on the chord line so that the painter can know how many square to paint on a line
58               int squares = squaresOnLineCount(chord,square.side());
59               paintRow(painter, square, squares);
60               nextRow(painter, square.side());
61               moved = moved+square.side();
62           }
63           // Make the method invariant with respect to painter position
64           painter.tl();
65           painter.mfd(circle.radius());
66       }
67   
68       // Move to next row
69       private void nextRow(SPainter painter, double rowHeight) {
70           painter.tr();
71           painter.mfd(rowHeight);
72           painter.tl();
73       }
74   
75       // Assumes the painter is at the center of the row to paint, facing right.
76       private void paintRow(SPainter painter, SSquare square, int squaresToPaint) {
77           double centerOffset= ((squaresToPaint*square.side())/2)- square.side()/2;
78           painter.mbk(centerOffset);
79   
80           // Paint the row of squares
81           int painted = 0;
82           while (painted < squaresToPaint){
83               paintOneSquare(painter,square);
84               painter.mfd(square.side());
85               painted = painted + 1;
86           }
87   
88           // Make the method invariant with respect to painter position.
89           painter.mbk(centerOffset + square.side());
90       }
91   
92       private void paintOneSquare(SPainter painter, SSquare square) {
93           square.s2();
94           SCircle circle = square.inscribingCircle();
95           painter.setColor(randomColor());
96           painter.paint(circle);
97           square.x2();
98           }
99   
100      private static Color randomColor() {
101          Random rgen = new Random();
102          int r = rgen.nextInt(256);
103          int g = rgen.nextInt(256);
104          int b = rgen.nextInt(256);
105          return new Color (r,g,b);
106      }
107  
108      // LineLength is the Chord Length, side Length is length of the square
109      private static int squaresOnLineCount(double lineLength, double sideLength) {
110          //Math.ceil return the smallest integer that is greater or equal to the input.
111          int squares = ((int)Math.ceil((lineLength-sideLength)/sideLength)+1);
112          return squares;
113      }
114  
115      private double chordLength(double yOffset, SCircle circle) {
116          double xLength = Math.sqrt(Math.pow(circle.radius(),2)-Math.pow(yOffset,2));
117          double chordLength = xLength * 2;
118          // chord length = 2 * Square root of (r2 * d*)
119          return chordLength;
120      }
121  
122      private static int getNumber(String prompt) {
123          // The input from the Input Dialog is a string so need to scanner to make sure it's a int.
124          String nss = JOptionPane.showInputDialog(null,prompt+"?");
125          Scanner scanner = new Scanner(nss);
126          return scanner.nextInt();
127      }
128  }
129