SimpleDots.java
/* 
 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 
 */
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 radius2 = getNumber("dot radius");
        Color circColor = getColor("dot 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);
        SCircle SmallCircle = new SCircle(radius2);
        // Paint the squares
        paintSimpleDots(painter, circle, SmallCircle, circColor);
    }
    private void paintSimpleDots(SPainter painter, SCircle circle, SCircle SmallCircle, Color circColor){
        // 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 = dotsOnLineCount(chord, SmallCircle.radius());
            paintRow(painter, SmallCircle, squares, circColor);
            nextRow(painter, SmallCircle.radius());
            moved = moved + SmallCircle.radius();
        }
        // 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, SCircle SmallCircle, int dotsToPaint, Color circColor){
        // Move backward 1/2 of the length we're painting to get ready to paint the row.
        double centerOffset = ( (dotsToPaint * SmallCircle.radius()) / 2) - SmallCircle.radius()/2;
        painter.mbk(centerOffset);
        // Paint the row of squares.
        int painted = 0;
        while (painted < dotsToPaint){
            paintOneDot(painter, SmallCircle, circColor);
            painter.mfd(SmallCircle.radius());
            painted = painted + 1;
        }
        // Make the method invariant with respect to painter position.
        painter.mbk(centerOffset + SmallCircle.radius());
    }
    private void paintOneDot(SPainter painter, SCircle SmallCircle, Color circColor){
        SmallCircle.s2();SmallCircle.s2();
        painter.setColor(circColor);
        painter.paint(SmallCircle);
        SmallCircle.x2();SmallCircle.x2();
    }
    private static int dotsOnLineCount(double lineLength, double sideLength){
        int dots = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
        return dots;
    }
    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 getColor(String prompt) {
        String color = JOptionPane.showInputDialog(null, prompt + "color?");
        if (color.equalsIgnoreCase("BLUE")) {
            return Color.blue;
        } else if (color.equalsIgnoreCase("RED")) {
            return Color.red;
        } else if (color.equalsIgnoreCase("GREEN")) {
            return Color.green;
        } else {
            return Color.BLACK;
        }
    }
    public SimpleDots() {
        paintTheImage();
    }
    public static void main (String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SimpleDots();
            }
        });
    }
}