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 HirstSquare { //*Main method call public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new HirstSquare(); } }); } // main method 의 main obj인 CircleOfSquares에 대한 method call. public HirstSquare() { paintTheImage(); } /**1**/ private void paintTheImage() { //Get the input information int radius = getNumber("circle radius"); int side = getNumber("square side length"); //Establish the painter SPainter painter = new SPainter("Circle of Squares", radius * 2 + 50, (radius * 2) + 50); painter.setBrushWidth(3); SCircle circle = new SCircle(radius); SSquare square = new SSquare(side); //paint the squares paintCircleOfSquares(painter, circle, square); } //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 paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square) { //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); //*need to define the fn, chordLength int squares = squaresOnLineCount(chord, square.side()); //*need to define the fn, squaresOnLineCount paintRow(painter, square, squares); //*need to define the command, paintRow nextRow(painter, square.side()); //*need to define the command, nextRow moved = moved + square.side(); //* 어떤 원 테두리 안에서만 순차적으로 줄바꿔 이동하며 그 원의 지름을 넘지 않는 한에서 //* 사각형을 생성하고 색을 입힌다. } //[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, SSquare square, int squaresToPaint) { //Move backward 1/2 of the length we're painting to get ready to paint the row. double centreOffset = ((squaresToPaint * square.side()) / 2) - square.side() / 2; painter.mbk(centreOffset); //Paint the row of squares int painted = 0; while (painted < squaresToPaint) { paintOneSquare(painter, square); painter.mfd(square.side()); painted = painted + 1; } //the method invariant to .mbk painter.mbk(centreOffset + square.side()); ///????: the parameter, squaresToPaint is undefined! } //2nd follow-up method call... for the command variable = paintOneSquare /**1-1-2-1**/ private static void paintOneSquare(SPainter painter, SSquare square) { square.s2(); painter.setColor(randomColor()); painter.paint(square); painter.setColor(Color.BLACK); painter.draw(square); square.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); } //[squaresOnLineCount 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 squaresOnLineCount(double lineLength, double sideLength) { int squares = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1); //the variable, squares, is already declared somewhere in previous sequence //so the utmost super method needs the prescribed return (squares), 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 squares; } /**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); }