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