HirstDots.java
/* 
 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed circles. 
 */

package npw;

import painter.SPainter;
import shapes.SCircle;

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

public class HirstDots {

    private void paintTheImage(){
        // Get the input information
        int radius = getNumber("circle radius");
        int radius1 = getNumber("circle radius");

        // Establish the painter
        SPainter painter = new SPainter("Circle of Dots", radius*2+50, radius*2+50);
        painter.setBrushWidth(3);
        SCircle circle = new SCircle(radius);
        SCircle circle1 = new SCircle(radius1);

        // Paint the circle
        paintHirstDots(painter, circle, circle1);
    }

    private void paintHirstDots(SPainter painter, SCircle circle, SCircle circle1){
        // Paint the circle of dots
        double howFarToMove = 0;

        while (howFarToMove < circle.radius()) {
            double chord = chordLength(howFarToMove, circle);
            int circles = circlesOnLineCount(chord, circle1.diameter());
            if (howFarToMove == 0) {
                paintRow(painter, circle1, circles);
            }
            else {
                // Stay invariant with respect to painter position
                painter.mfd(howFarToMove);
                paintRow(painter, circle1, circles);
                painter.mbk(howFarToMove * 2);
                paintRow(painter, circle1, circles);
                painter.mfd(howFarToMove);
            }
            howFarToMove = howFarToMove + circle1.diameter();
        }
    }

    // Assumes the painter is at the center of the row to paint, facing right.
    private void paintRow(SPainter painter, SCircle circle1, int circlesToPaint){
        // Turn right to start painting the row.
        painter.tr();
        // Move backward 1/2 of the length we're painting to get ready to paint the row.
        double centerOffset = ( (circlesToPaint * circle1.diameter()) / 2) - circle1.diameter()/2;
        painter.mbk(centerOffset);

        // Paint the row of circles.
        int painted = 0;
        while (painted < circlesToPaint){
            paintOneCircle(painter, circle1);
            painter.mfd(circle1.diameter());
            painted = painted + 1;
        }

        // Make the method invariant with respect to painter position.
        painter.mbk(centerOffset + circle1.diameter());
        // Turn left to stay invariant with respect to direction.
        painter.tl();
    }

    private void paintOneCircle(SPainter painter, SCircle circle1){
        circle1.s2();
        painter.setColor(randomColor());
        painter.paint(circle1);
        circle1.x2();

    }

    private static int circlesOnLineCount(double lineLength, double diameter){
        int circles = ( (int)Math.ceil( (lineLength - diameter ) / diameter ) + 1);
        return circles;
    }

    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 HirstDots() {
        paintTheImage();
    }

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