SimpleDots.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.Scanner;

public class SimpleDots {

    private void paintTheImage() {
        // Get the input information
        int radius = getNumber("circle radius");
        int radius1 = getNumber("circle radius");
        String nss = JOptionPane.showInputDialog(null, "color of circle?");

        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
        paintSimpleDots(painter, circle, circle1, nss);
    }

    private void paintSimpleDots(SPainter painter, SCircle circle, SCircle circle1, String nss){
        // 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, nss);
            }
            else {
                // Stay invariant with respect to painter position
                painter.mfd(howFarToMove);
                paintRow(painter, circle1, circles, nss);
                painter.mbk(howFarToMove * 2);
                paintRow(painter, circle1, circles, nss);
                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, String nss){
        // 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, nss);
            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, String nss){
        circle1.s2();
        if (nss.equalsIgnoreCase("red")){
            painter.setColor(Color.red);
        } else if (nss.equalsIgnoreCase("blue")) {
            painter.setColor(Color.blue);
        }else if (nss.equalsIgnoreCase("green")) {
            painter.setColor(Color.green);
        }else {
            painter.setColor(Color.black);
        }
        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();
    }
    
    public SimpleDots() {
        paintTheImage();
    }

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