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