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