HirstDots.java
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 HirstDots {
    //*Main method call
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new HirstDots();
            }
        });
    }

    // main method 의 main obj인 CircleOfDots에 대한 method call.

    public HirstDots() {
        paintTheImage();
    }

    /**1**/
    private void paintTheImage() {
        //Get the input information
        int radius = getNumber("circle radius");
        int dotR = getNumber("Dot r length");

        //Establish the painter
        SPainter painter = new SPainter("Circle of Dots", radius * 2 + 50, (radius * 2) + 50);
        painter.setBrushWidth(3);
        SCircle circle = new SCircle(radius);
        SCircle dot = new SCircle(dotR);

        //paint the dots
        paintCircleOfDots(painter, circle, dot);
    }

    //follow-up method call ... [getNumber(f) method]

    /******************************************************
     * [method name (method type) method]
     *  f: function, c: command
     ******************************************************/
    /******************************************************
     * The static keyword in Java means that the variable or function is shared
     * ...between all instances of that class as it belongs to the type,
     * ...not the actual objects themselves.
     * 즉, static이란 꼬리표(label)은 그 method action을 해당 method 이하의 객체들로 한정시킨다는말.
     * 한편, static method 이하에서 정의된 객체들이나 그 method 자체도 다른 곳에서 쓰일경우,
     * 그 이름의 꼬리표에 해당하는 method의 정의로 redirected 됨.
     */

    /**1-4**/
    private static int getNumber(String prompt) {
        String nss = JOptionPane.showInputDialog(null, prompt + "?");
        Scanner scanner = new Scanner(nss);
        //??? How its return can be an int?
        return scanner.nextInt();
    }

//*Describing (Construct) what the paintCircleOfSquares method is ...
// ... needs 3 objs included in that method's parameters
// as variables of three, also it needs 3 variable types corresponded to variables in the parameter.

    /**1-1**/
    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);
            //*need to define the fn, chordLength
            int dots = dotsOnLineCount(chord, dot.diameter());
            //*need to define the fn,  dotsOnLineCount
            paintRow(painter, dot, dots);
            //*need to define the command, paintRow
            nextRow(painter, dot.diameter());
            //*need to define the command, nextRow
            moved = moved + dot.diameter();
            //* 어떤 원 테두리 안에서만 순차적으로 줄바꿔 이동하며 그 원의 지름을 넘지 않는 한에서
            //* 사각형을 생성하고 색을 입힌다.
        }
        //[Invariant w/ respect to the painter position] Make the method invariant with respect to painter position
        painter.tl();
        painter.mfd(circle.radius());
    }
    //one of the follow-up methods of the super method: painterCircleOfSquares
    //the super method needs three sub-methods: chordLength, squaresOnLineCount, paintRow, and nextRow.
    //+a: each sub-methods have each own parameters, as objects thrown on the field,
    // which defines their method that user wants to command to do something primarily aimed for.
    //  ... or to define generating values.

    //Move to the next row [nextRow method]
    //+a: in the parenthesis referring the parameters, you'd better refer types of the parameters

    /**1-1-1**/
    private static void nextRow(SPainter painter, double rowHeight) {
        painter.tr();
        painter.mfd(rowHeight);
        painter.tl();
    }

    /**1-1-2**/
    //Assumes the painter is at the center of the row to paint, facing right.
    //[paintRow method]
    private static void paintRow(SPainter painter, SCircle dot, int dotsToPaint) {
        //Move backward 1/2 of the length we're painting to get ready to paint the row.
        double centreOffset = ((dotsToPaint * dot.diameter()) / 2) - dot.diameter() / 2;
        //**WHAT IS THIS?? HOW it is constructed in this way??**//
        painter.mbk(centreOffset);

        //Paint the row of dots
        int painted = 0;
        while (painted < dotsToPaint) {
            paintOneDot(painter, dot);
            painter.mfd(dot.diameter());
            painted = painted + 1;
        }
        //the method invariant to .mbk1
        painter.mbk(centreOffset + dot.diameter());

        ///????: the parameter, dotsToPaint is undefined!
    }
    //2nd follow-up method call... for the command variable = paintOneDot

    /**1-1-2-1**/
    private static void paintOneDot(SPainter painter, SCircle dot) {
        dot.s2();
        painter.setColor(randomColor());
        painter.paint(dot);
        dot.x2();
    }
    //3rd follow-up method call... [randomColor method]

    /*******************************************************************************
     /// -b/c that method-function, randomColor() is one and only
     /// ... parameter of .setColor -> type: Color
     //// -REASON: randomColor method has the empty parameter;
     //// ... the fact declares it follows the possible types of the super command.
     *******************************************************************************/

    /**1-1-2-1-1**/
    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);
    }

    //[dotsOnLineCount method]
    /***************************************************************************
     //+a: always look closer when you start to make new method.
     // ...Binding that new method to the previously defined identity of the method, in the super method.
     // ... ex) if it needs some types of returns, then remind
     // ... [0]. it's a function
     // ... [1]. it needs a particular type as defined beforehand,
     // ... [2]. which variables that includes in for its parameters.
     *****************************************************************************/

    /**1-2**/
    private static int dotsOnLineCount(double lineLength, double dotR) {
        int dots = ((int) Math.ceil((lineLength - dotR) / dotR) + 1);
        //the variable, dots, is already declared somewhere in previous sequence
        //so the utmost super method needs the prescribed return (dots), and
        /// in any case, function methods need a return where the return is
        /// whether one of the parameter which constructs its method,
        /// ... or the return must be defined by the parameters prescribed like above.
        return dots;
    }

    /**1-3**/
    // [chordLength method]
    private static 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;
    }

    //**서순에 구애받지 않게 static을 붙여두는거다.
//    /**1-4**/
//    private static int getNumber (String prompt) {
//        String nss = JOptionPane.showInputDialog(null, prompt+"?");
//        Scanner scanner = new Scanner(nss);
//        //??? How its return can be an int?
//        return scanner.nextInt();}
//
//    /**1-1-2-1-1*/
//    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);
}