1 2 package npw; 3 4 import painter.SPainter; 5 import shapes.SCircle; 6 import shapes.SSquare; 7 8 import javax.swing.*; 9 import java.awt.*; 10 import java.util.Random; 11 import java.util.Scanner; 12 13 public class HirstSquares { 14 15 private void paintTheImage(){ 16 // Get the input information 17 int radius = getNumber("circle radius"); 18 int side = getNumber("square side length"); 19 20 // Establish the painter 21 SPainter painter = new SPainter("HirstSquares", radius*2+50, radius*2+50); 22 painter.setBrushWidth(3); 23 SCircle circle = new SCircle(radius); 24 SSquare square = new SSquare(side); 25 26 // Paint the squares 27 paintHirstSquares(painter, circle, square); 28 } 29 30 private void paintHirstSquares(SPainter painter, SCircle circle, SSquare square){ 31 // Position the painter to begin drawing the rows 32 painter.mfd(circle.radius()); 33 painter.tr(); 34 // Paint the Hirst squares 35 double moved = 0; 36 while (moved < circle.diameter()) { 37 double chord = chordLength(circle.radius() - moved, circle); 38 int squares = squaresOnLineCount(chord, square.side()); 39 paintRow(painter, square, squares); 40 nextRow(painter, square.side()); 41 moved = moved + square.side(); 42 } 43 // Make the method invariant with respect to painter position 44 painter.tl(); 45 painter.mfd(circle.radius()); 46 } 47 48 // Move to the next row 49 private void nextRow(SPainter painter, double rowHeight){ 50 painter.tr(); 51 painter.mfd(rowHeight); 52 painter.tl(); 53 } 54 55 // Assumes the painter is at the center of the row to paint, facing right. 56 private void paintRow(SPainter painter, SSquare square, int squaresToPaint){ 57 // Move backward 1/2 of the length we're painting to get ready to paint the row. 58 double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2; 59 painter.mbk(centerOffset); 60 61 // Paint the row of squares. 62 int painted = 0; 63 while (painted < squaresToPaint){ 64 paintOneSquare(painter, square); 65 painter.mfd(square.side()); 66 painted = painted + 1; 67 } 68 69 // Make the method invariant with respect to painter position. 70 painter.mbk(centerOffset + square.side()); 71 } 72 73 private void paintOneSquare(SPainter painter, SSquare square){ 74 square.s2(); 75 painter.setColor(randomColor()); 76 painter.paint(square); 77 painter.setColor(Color.BLACK); 78 painter.draw(square); 79 square.x2(); 80 81 82 } 83 84 private static int squaresOnLineCount(double lineLength, double sideLength){ 85 int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 86 return squares; 87 } 88 89 private double chordLength(double yOffset, SCircle circle){ 90 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 91 double chordLength = xLength * 2; 92 return chordLength; 93 } 94 95 private static int getNumber(String prompt) { 96 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 97 Scanner scanner = new Scanner(nss); 98 return scanner.nextInt(); 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 public HirstSquares() { 110 paintTheImage(); 111 } 112 113 public static void main(String[] args) { 114 SwingUtilities.invokeLater(new Runnable() { 115 public void run() { 116 new HirstSquares(); 117 } 118 }); 119 } 120 } 121 122 123 124 125