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