HirstSquares.java
/* 
1 
2   a program to paint, centered on the canvas, a circle of randomly colored, black-framed squares, with spaces. 
3 
*/

package npw;

import painter.SPainter;
import shapes.SCircle;
import shapes.SSquare;

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.Scanner;


public class HirstSquares {
    private void paintTheImage() {
        // get the input info
        int radius = getNumber("circle radius");
        int side = getNumber("square side length");
        // establish painter
        SPainter painter = new SPainter("HirstSquares", radius * 2 + 50, radius * 2 + 50);
        painter.setBrushWidth(3);
        SCircle circle = new SCircle(radius);
        SSquare square = new SSquare(side);
        // paint the squares
        paintCircleOfSquares(painter, circle, square);

    }


    private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square) {
        // position the painter to begin drawing the rows
        painter.mfd(circle.radius());
        painter.tr();
        // paint the circle of squares
        double moved = 0;
        while (moved < circle.diameter()) {
            double chord = chordLength(circle.radius() - moved, circle);
            int squares = squaresOnLineCount(chord, square.side());
            paintRow(painter, square, squares);
            nextRow(painter, square.side());
            moved = moved + square.side();

        }
        // make the method invariant with respect to painter position
        painter.tl();
        painter.mfd(circle.radius());

    }

    // move to the next row


    private void nextRow(SPainter painter, double rowHeight) {
        painter.tr();
        painter.mfd(rowHeight);
        painter.tl();

    }

    // assumes the painter is at the center of the row to paint, facing right.


    private void paintRow(SPainter painter, SSquare square, int squaresToPaint) {
        // move backward one half of the length we're painting to get ready to paint the row
        double centerOffset = ((squaresToPaint * square.side()) / 2) - square.side() / 2;
        painter.mbk(centerOffset);
        // paint the row of squares
        int painted = 0;
        while (painted < squaresToPaint) {
            paintOneSquare(painter, square);
            painter.mfd(square.side());
            painted = painted + 1;

        }
        // make the method invariant with respect to painter position
        painter.mbk(centerOffset + square.side());

    }


    private void paintOneSquare(SPainter painter, SSquare square) {
        square.shrink(10);
        painter.setColor(randomColor());
        painter.paint(square);
        painter.setColor(Color.black);
        painter.draw(square);
        square.expand(10);

    }


    private static int squaresOnLineCount(double lineLength, double sideLength) {
        int squares = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1);
        return squares;

    }


    private double chordLength(double yOffset, SCircle circle) {
        double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
        double chordLength = xLength * 2;
        return chordLength;

    }


    private static int getNumber(String prompt) {
        String nss = JOptionPane.showInputDialog(null, prompt + "?");
        Scanner scanner = new Scanner(nss);
        return scanner.nextInt();

    }


    private static Color randomColor() {
        Random rgen = new Random();
        int r = rgen.nextInt(256);
        int g = rgen.nextInt(256);
        int b = rgen.nextInt(256);
        return new Color(r, g, b);

    }


    public HirstSquares() {
        paintTheImage();

    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new HirstSquares();

            }
        });

    }
}