1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 3 */ 4 package npw; 5 6 import painter.SPainter; 7 import shapes.SCircle; 8 import shapes.SSquare; 9 import javax.swing.*; 10 import java.awt.*; 11 import java.util.Random; 12 import java.util.Scanner; 13 14 public class CircleOfSquares { 15 16 private void paintTheImage() { 17 18 // Get the input information 19 int radius = getNumber("circle radius"); 20 int side = getNumber("square side length"); 21 22 // Establish the painter 23 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 24 painter.setBrushWidth(3); 25 SCircle circle = new SCircle(radius); 26 SSquare square = new SSquare(side); 27 28 // Paint the squares 29 paintCircleOfSquares(painter, circle, square); 30 } 31 32 private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square) { 33 34 // Position the painter to begin drawing the rows 35 painter.mfd(circle.radius()); 36 painter.tr(); 37 38 // Paint the circle of squares 39 double moved = 0; 40 while (moved < circle.diameter()) { 41 double chord = chordLength(circle.radius() - moved, circle); 42 int squares = squaresOnLineCount(chord, square.side()); 43 paintRow(painter, square, squares); 44 nextRow(painter, square.side()); 45 moved = moved + square.side(); 46 } 47 48 // Make the method invariant with respect to painter position 49 painter.tl(); 50 painter.mfd(circle.radius()); 51 } 52 53 // Move to the next row 54 private void nextRow(SPainter painter, double rowHeight) { 55 56 painter.tr(); 57 painter.mfd(rowHeight); 58 painter.tl(); 59 } 60 61 // Assumes the painter is at the center of the row to paint, facing right 62 private void paintRow(SPainter painter, SSquare square, int squaresToPaint) { 63 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 82 painter.setColor(randomColor()); 83 painter.paint(square); 84 painter.setColor(Color.BLACK); 85 painter.draw(square); 86 } 87 88 private static int squaresOnLineCount(double lineLength, double sideLength) { 89 90 int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 91 return squares; 92 } 93 94 private double chordLength(double yOffset, SCircle circle) { 95 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 109 Random rgen = new Random(); 110 int r = rgen.nextInt(256); 111 int g = rgen.nextInt(256); 112 int b = rgen.nextInt(256); 113 return new Color(r,g,b); 114 } 115 116 public CircleOfSquares() { 117 paintTheImage(); 118 } 119 120 public static void main(String[] args) { 121 SwingUtilities.invokeLater(new Runnable() { 122 public void run() { 123 new CircleOfSquares(); 124 } 125 }); 126 } 127 }