SimpleDots.java
/* 
 * a program to paint, centered on the canvas, a circle of dots of a given color, with spaces. 
 */

   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 info
           int radius = getNumber("circle radius");
           int rad = getNumber("dot radius");
           // establish painter
           SPainter painter = new SPainter("SimpleDots", radius*2+50, radius*2+50);
           changeColor(painter);
           painter.setBrushWidth(3);
           SCircle circle = new SCircle(radius);
           SCircle dot = new SCircle(rad);
           // paint the dots
           paintCircleOfDots(painter, circle, dot);
       }

       private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot) {
           // 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);
               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) {
           // move backward one half 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 squares
           int painted = 0;
           while (painted < dotsToPaint) {
               paintOneDot(painter, dot);
               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) {
           dot.shrink(10);
           painter.paint(dot);
           dot.expand(10);
       }

       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 void changeColor(SPainter painter) {
           String choose = JOptionPane.showInputDialog(null,"red, blue, or green?");
           Scanner scanner = new Scanner(choose);
           if (choose.equalsIgnoreCase("blue")) {
               painter.setColor(Color.blue);
           } else if (choose.equalsIgnoreCase("red")) {
               painter.setColor(Color.red);
           } else if (choose.equalsIgnoreCase("green")) {
               painter.setColor(Color.green);
           } else {
               painter.setColor(Color.black);
           }
       }

       public SimpleDots() {
           paintTheImage();
       }

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