1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares 3 * with a gap in between each square. 4 */ 5 6 package npw; 7 8 import painter.SPainter; 9 import shapes.SCircle; 10 import javax.swing.*; 11 import java.awt.*; 12 import java.util.Random; 13 import java.util.Scanner; 14 15 public class HirstDots { 16 17 private void paintTheImage() { 18 19 // Get the input information 20 int radius = getNumber("circle radius"); 21 int radii = getNumber("circle radii"); 22 23 // Establish the painter 24 SPainter painter = new SPainter("Circle of Circles", radius*2+50, radius*2+50); 25 painter.setBrushWidth(3); 26 SCircle circle = new SCircle(radius); 27 SCircle circles = new SCircle(radii); 28 29 // Paint the circles 30 paintHirstDots(painter, circle, circles); 31 } 32 33 private void paintHirstDots(SPainter painter, SCircle circle, SCircle circles) { 34 35 // Position the painter to begin drawing the rows 36 painter.mfd(circle.radius()); 37 painter.tr(); 38 39 // Paint the circle of circles 40 double moved = 0; 41 while (moved < circle.diameter()) { 42 double chord = chordLength(circle.radius() - moved, circle); 43 int circNum = circlesOnLineCount(chord, circles.diameter()); 44 paintRow(painter, circles, circNum); 45 nextRow(painter, circles.diameter()); 46 moved = moved + circles.diameter(); 47 } 48 49 // Make the method invariant with respect to painter position 50 painter.tl(); 51 painter.mfd(circle.radius()); 52 } 53 54 // Move to the next row 55 private void nextRow(SPainter painter, double rowHeight) { 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, SCircle circles, int circlesToPaint) { 63 64 // Move backward 1/2 of the length we're painting to get ready to paint the row. 65 double centerOffset = ( (circlesToPaint * circles.diameter()) / 2) - circles.diameter() / 2; 66 painter.mbk(centerOffset); 67 68 // Paint the row of circles 69 int painted = 0; 70 while (painted < circlesToPaint) { 71 paintOneSquare(painter, circles); 72 painter.mfd(circles.diameter()); 73 painted = painted + 1; 74 } 75 76 // Make the method invariant with respect to painter position 77 painter.mbk(centerOffset + circles.diameter()); 78 } 79 80 private void paintOneSquare(SPainter painter, SCircle circles) { 81 82 circles.s2(); 83 painter.setColor(randomColor()); 84 painter.paint(circles); 85 circles.x2(); 86 } 87 88 private static int circlesOnLineCount(double lineLength, double sideLength) { 89 90 int circNum = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 91 return circNum; 92 } 93 private double chordLength(double yOffset, SCircle circle) { 94 95 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 96 double chordLength = xLength * 2; 97 return chordLength; 98 } 99 100 private static int getNumber(String prompt) { 101 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 HirstDots() { 117 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 }