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