1 /* 2 * Assignment 4 Problem 2: Circle Of Squares 3 * A program to paint, centered on the canvas, a circle of randomly colored,black-framed squares 4 */ 5 6 package npw; 7 8 import painter.SPainter; 9 import shapes.SCircle; 10 import shapes.SSquare; 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Random; 14 import java.util.Scanner; 15 16 17 public class CircleOfSquares { 18 public static void main(String[] args){ 19 SwingUtilities.invokeLater(new Runnable() { 20 public void run() { 21 new CircleOfSquares(); 22 } 23 }); 24 } 25 public CircleOfSquares() { 26 paintTheImage(); 27 } 28 29 private void paintTheImage() { 30 // Get the input information 31 // create new method named "getNumber" to scan from input 32 int radius = getNumber("circle radius"); 33 int side = getNumber("square side length"); 34 35 // Establish the painter 36 // Create new painter with Canvas names "Circle of Squares" and side of Circle diameter plus 50 37 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 38 // Painter's brushWidth widen so that the side of the square could be more visible 39 painter.setBrushWidth(3); 40 SCircle circle = new SCircle(radius); 41 SSquare square = new SSquare(side); 42 43 // Paint the squares 44 // Created new method named "paintCircleOfSquares" with parameter of painter,circle,and square 45 paintCircleOfSquares(painter, circle, square); 46 } 47 48 private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square) { 49 // Position the painter to begin drawing the rows 50 painter.mfd(circle.radius()); // painter move forward as circle radius 51 painter.tr(); 52 // Paint the circle of squares 53 double moved = 0; 54 while (moved < circle.diameter()){ 55 double chord = chordLength(circle.radius()-moved,circle); 56 // number of square on the chord line so that the painter can know how many square to paint on a line 57 int squares = squaresOnLineCount(chord,square.side()); 58 paintRow(painter, square, squares); 59 nextRow(painter, square.side()); 60 moved = moved+square.side(); 61 } 62 // Make the method invariant with respect to painter position 63 painter.tl(); 64 painter.mfd(circle.radius()); 65 } 66 67 // Move to next row 68 private void nextRow(SPainter painter, double rowHeight) { 69 painter.tr(); 70 painter.mfd(rowHeight); 71 painter.tl(); 72 } 73 74 // Assumes the painter is at the center of the row to paint, facing right. 75 private void paintRow(SPainter painter, SSquare square, int squaresToPaint) { 76 double centerOffset= ((squaresToPaint*square.side())/2)- square.side()/2; 77 painter.mbk(centerOffset); 78 79 // Paint the row of squares 80 int painted = 0; 81 while (painted < squaresToPaint){ 82 paintOneSquare(painter,square); 83 painter.mfd(square.side()); 84 painted = painted + 1; 85 } 86 87 // Make the method invariant with respect to painter position. 88 painter.mbk(centerOffset + square.side()); 89 } 90 91 private void paintOneSquare(SPainter painter, SSquare square) { 92 painter.setColor(randomColor()); 93 painter.paint(square); 94 painter.setColor(Color.BLACK); 95 painter.draw(square); 96 } 97 98 private static Color randomColor() { 99 Random rgen = new Random(); 100 int r = rgen.nextInt(256); 101 int g = rgen.nextInt(256); 102 int b = rgen.nextInt(256); 103 return new Color (r,g,b); 104 } 105 106 // LineLength is the Chord Length, side Length is length of the square 107 private static int squaresOnLineCount(double lineLength, double sideLength) { 108 //Math.ceil return the smallest integer that is greater or equal to the input. 109 int squares = ((int)Math.ceil((lineLength-sideLength)/sideLength)+1); 110 return squares; 111 } 112 113 private double chordLength(double yOffset, SCircle circle) { 114 double xLength = Math.sqrt(Math.pow(circle.radius(),2)-Math.pow(yOffset,2)); 115 double chordLength = xLength * 2; 116 // chord length = 2 * Square root of (r2 * d*) 117 return chordLength; 118 } 119 120 private static int getNumber(String prompt) { 121 // The input from the Input Dialog is a string so need to scanner to make sure it's a int. 122 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 123 Scanner scanner = new Scanner(nss); 124 return scanner.nextInt(); 125 } 126 } 127