1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, unframed dots 3 * that are separated. 4 * */ 5 6 package npw; 7 8 import painter.SPainter; 9 import shapes.SCircle; 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 dotRadius = getNumber("dot 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 dot = new SCircle(dotRadius); 28 29 // Paint the dots 30 paintHirstDots(painter, circle, dot); 31 } 32 33 private void paintHirstDots(SPainter painter, SCircle circle, SCircle dot) { 34 // Position the painter to begin drawing the rows 35 painter.mfd(circle.radius()); 36 painter.tr(); 37 // Paint the circle of squares 38 double moved = 0; 39 while (moved < circle.diameter()) { 40 double chord = chordLength(circle.radius() - moved, circle); 41 int dots = dotsOnLineCount(chord, dot.diameter()); 42 paintRow(painter, dot, dots); 43 nextRow(painter, dot.diameter()); 44 moved = moved + dot.diameter(); 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 dot, int dotsToPaint) { 60 // Move backward 1/2 of the length we're painting to get ready to paint the row. 61 double centerOffset = ((dotsToPaint * dot.diameter()) / 2) - dot.diameter() / 2; 62 painter.mbk(centerOffset); 63 64 // Paint the row of squares. 65 int painted = 0; 66 while (painted < dotsToPaint) { 67 paintOneDot(painter, dot); 68 painter.mfd(dot.diameter()); 69 painted = painted + 1; 70 } 71 72 // Make the method invariant with respect to painter position. 73 painter.mbk(centerOffset + dot.diameter()); 74 } 75 76 private void paintOneDot(SPainter painter, SCircle dot) { 77 dot.s2(); 78 painter.setColor(randomColor()); 79 painter.paint(dot); 80 dot.x2(); 81 } 82 83 private static int dotsOnLineCount(double lineLength, double diameterLength) { 84 int dots = ((int) Math.ceil((lineLength - diameterLength) / diameterLength) + 1); 85 return dots; 86 } 87 88 private double chordLength(double yOffset, SCircle circle) { 89 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 90 double chordLength = xLength * 2; 91 return chordLength; 92 } 93 94 private static int getNumber(String prompt) { 95 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 96 Scanner scanner = new Scanner(nss); 97 return scanner.nextInt(); 98 } 99 100 private static Color randomColor() { 101 Random rgen = new Random(); 102 int r = rgen.nextInt(256); 103 int g = rgen.nextInt(256); 104 int b = rgen.nextInt(256); 105 return new Color(r, g, b); 106 } 107 108 public HirstDots() { 109 paintTheImage(); 110 } 111 112 public static void main(String[] args) { 113 SwingUtilities.invokeLater(new Runnable() { 114 public void run() { 115 new HirstDots(); 116 } 117 }); 118 } 119 } 120