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