SimpleDots.java
/* 
 ** A Program to paint many circle to form big circle of one specific color;red ,green,blue; or black which are 
 *seperated dots from each other 
 
 */

package npw;

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

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

public class SimpleDots {
    SimpleDots() {
        paintTheImage();
    }

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

    private void paintTheImage() {
        //Get the input information
        int radius = getNumber("Circle radius");
        int side = getNumber("square side length");
        String color= getString("circle color");

        //Establish the painter
        SPainter painter = new SPainter("SimpleDots", radius * 2 + 50, radius * 2 + 50);
        painter.setBrushWidth(0);
        SCircle circle = new SCircle(radius);
        SSquare square = new SSquare(side);

        //paint the squares

        paintCircleOfSquares(painter, circle, square, color);
    }

    private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square, String color) {
        //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 = squareOnLineCount(chord, square.side());
            paintRow(painter, square, squares, color);
            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();
    }

    //Assume the painter is at the center of the row to paint, facing right.
    private void paintRow(SPainter painter, SSquare square, int squaresToPaint, String color) {
        //move backward 1/2 of the length we're painting to get ready to paint the row.
        double centerOfset = ((squaresToPaint * square.side()) / 2) - square.side() / 2;
        painter.mbk(centerOfset);

        //paint the row of squares.
        int painted = 0;
        while (painted < squaresToPaint) {
            paintOneSquare(painter, square, color);
            painter.mfd(square.side());
            painted = painted + 1;
        }

        //Make the  method invariant with respect to painter position.Deterministic Invention
        painter.mbk(centerOfset + square.side());
    }

    private void paintOneSquare(SPainter painter, SSquare square, String color) {
        square.s2();
        SCircle circle = square.inscribingCircle();
        if (color.equals("red")){
            painter.setColor(Color.red);
            painter.paint(circle);
        }
        else if (color.equals("blue")){
            painter.setColor(Color.BLUE);
            painter.paint(circle);
        }
        else if (color.equals("green")){
            painter.setColor(Color.green);
            painter.paint(circle);
        }else{
            painter.setColor(Color.black);
            painter.paint(circle);
        }


        square.expand(square.side());
    }

    private static int squareOnLineCount(double lineLength, double sideLenth) {
        int squares = ((int) Math.ceil((lineLength - sideLenth) / sideLenth) + 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();
    }

    private static String getString(String prompt) {
        String nss = JOptionPane.showInputDialog(null, prompt + "?");
        Scanner scanner = new Scanner(nss);
        return scanner.next();
    }
}