SimpleDots.java
/* 
 * A program to paint, centered on the canvas, a circle of simple dots. 
 */

package npw;

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

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 side = getNumber("square side length");
        String command = JOptionPane.showInputDialog(null, "Color?");
        //Establish the painter
        SPainter painter = new SPainter("Circle of Squares", 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,command);
    }
    private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square,String command) {
        //Position the painter to begin 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,command);
            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,String command) {
        //Move backward 1/2 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,command);
            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,String command) {
        square.s2();
        SCircle dots = square.inscribingCircle();
            if (command.equalsIgnoreCase("blue")) {
                painter.setColor(Color.BLUE);
            } else if (command.equalsIgnoreCase("red")) {
                painter.setColor(Color.RED);
            } else if (command.equalsIgnoreCase("green")) {
                painter.setColor(Color.GREEN);
            } else {
                painter.setColor(Color.BLACK);
            }
            painter.paint(dots);
            square.x2();
        }
    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();
    }
    public SimpleDots() {
        paintTheImage();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SimpleDots();
            }
        });
    }
}