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