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