1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 import shapes.SSquare; 6 7 import javax.swing.*; 8 import java.awt.*; 9 import java.util.Random; 10 import java.util.Scanner; 11 12 public class CircleOfSquares { 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 45 // Move to the next row 46 private void nextRow(SPainter painter, double rowHeight){ 47 painter.tr(); 48 painter.mfd(rowHeight); 49 painter.tl(); 50 } 51 52 // Assumes the painter is at the center of the row to paint, facing right. 53 private void paintRow(SPainter painter, SSquare square, int squaresToPaint){ 54 // Move backward 1/2 of the length we're painting to get ready to paint the row. 55 double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2; 56 painter.mbk(centerOffset); 57 58 // Paint the row of squares. 59 int painted = 0; 60 while (painted < squaresToPaint){ 61 paintOneSquare(painter, square); 62 painter.mfd(square.side()); 63 painted = painted + 1; 64 } 65 66 // Make the method invariant with respect to painter position. 67 painter.mbk(centerOffset + square.side()); 68 } 69 70 private void paintOneSquare(SPainter painter, SSquare square){ 71 painter.setColor(randomColor()); 72 painter.paint(square); 73 painter.setColor(Color.BLACK); 74 painter.draw(square); 75 } 76 77 private static int squaresOnLineCount(double lineLength, double sideLength){ 78 int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 79 return squares; 80 } 81 82 private double chordLength(double yOffset, SCircle circle){ 83 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 84 double chordLength = xLength * 2; 85 return chordLength; 86 } 87 88 private static int getNumber(String prompt) { 89 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 90 Scanner scanner = new Scanner(nss); 91 return scanner.nextInt(); 92 } 93 94 private static Color randomColor() { 95 Random rgen = new Random(); 96 int r = rgen.nextInt(256); 97 int g = rgen.nextInt(256); 98 int b = rgen.nextInt(256); 99 return new Color(r,g,b); 100 } 101 102 public CircleOfSquares() { 103 paintTheImage(); 104 } 105 106 public static void main(String[] args) { 107 SwingUtilities.invokeLater(new Runnable() { 108 public void run() { 109 new CircleOfSquares(); 110 } 111 }); 112 } 113 } 114