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