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