SimpleDots.java
/* 
 * A program to paint, centered on the canvas, a circle of randomly colored,  hirst dots with spaces of specific colors. Black if none chosen 
 */

package npw;

import painter.SPainter;
import shapes.SCircle;

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

public class SimpleDots {

    private void paintTheImage(){
        // Get the input information
        int radius = getNumber("circle radius");
        int dotRadius = getNumber("dot radius");
        Color userColor = getColor("Please type a color that is either red, blue, or green");
        // 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 dot = new SCircle(dotRadius);
        // Paint the dots
        paintCircleOfdots(painter, circle, dot, userColor);
    }


    private void paintCircleOfdots(SPainter painter, SCircle circle, SCircle dot, Color userColor){
        // Position the painter to begin drawing the rows
        painter.mfd(circle.radius());
        painter.tr();
        // Paint the circle of dots
        double moved = 0;
        while (moved < circle.diameter()) {
            double chord = chordLength(circle.radius() - moved, circle);
            int dots = dotsOnLineCount(chord, dot.diameter());
            paintRow(painter, dot, dots, userColor);
            nextRow(painter, dot.diameter());
            moved = moved + dot.diameter();
        }
        // 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 dot, int dotsToPaint, Color userColor){
        // Move backward 1/2 of the length we're painting to get ready to paint the row.
        double centerOffset = ( (dotsToPaint * dot.diameter()) / 2) - dot.diameter() /2;
        painter.mbk(centerOffset);

        // Paint the row of dots.
        int painted = 0;
        while (painted < dotsToPaint){
            paintOneDot(painter, dot, userColor);
            painter.mfd(dot.diameter());
            painted = painted + 1;
        }

        // Make the method invariant with respect to painter position.
        painter.mbk(centerOffset + dot.diameter());
    }

    private void paintOneDot(SPainter painter, SCircle dot, Color userColor){
        dot.s2(); //shrink twice to get size appropriate
        painter.setColor(userColor);
        painter.paint(dot);

        dot.x2();
    }

    private static int dotsOnLineCount(double lineLength, double diameter){
        int dots = ( (int)Math.ceil( ( lineLength - diameter ) / diameter ) + 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 Color getColor(String prompt) {
        String nss = JOptionPane.showInputDialog(null, prompt+":");
        Scanner scanner = new Scanner(nss);

        String color = scanner.next();
        if (color.equalsIgnoreCase("red")) {
            return Color.red;
        } else if (color.equalsIgnoreCase("blue")) {
            return Color.blue;
        } else if (color.equalsIgnoreCase("green")) {
            return Color.green;
        } else {
            return Color.black;
        }


    }

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

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