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 { 12 13 private void paintTheImage(){ 14 //Get the input information 15 int radius = getNumber("circle radius"); 16 int dotRadius = getNumber("dot radius"); 17 18 //Establish the painter 19 SPainter painter = new SPainter("Hirst Dots", radius*2+50, radius*2+50); 20 painter.setBrushWidth(3); 21 SCircle circle = new SCircle(radius); 22 SCircle dot = new SCircle(dotRadius); 23 24 //Paint the squares 25 paintHirstDots(painter, circle, dot); 26 } 27 28 private void paintHirstDots(SPainter painter, SCircle circle, SCircle dot) { 29 //Position the painter to begin drawing the rows 30 painter.mfd(circle.radius()); 31 painter.tr(); 32 //Paint the circle of squares 33 double moved = 0; 34 while (moved < circle.diameter()) { 35 double chord = chordLength(circle.radius() - moved, circle); 36 int dots = dotsOnLineCount(chord, dot.diameter()); 37 paintRow(painter, dot, dots); 38 nextRow(painter, dot.diameter()); 39 moved = moved + dot.diameter(); 40 } 41 //Make the method invariant with respect to painter position 42 painter.tl(); 43 painter.mfd(circle.radius()); 44 } 45 46 //Move to the next row 47 private void nextRow(SPainter painter, double rowHeight){ 48 painter.tr(); 49 painter.mfd(rowHeight); 50 painter.tl(); 51 } 52 53 //Assumes the painter is at the center of the row to paint, facing right. 54 private void paintRow(SPainter painter, SCircle dot, int squaresToPaint){ 55 //Move backward 1/2 of the length we're painting to get ready to paint the row. 56 double centerOffset = ( (squaresToPaint * dot.diameter()) / 2) - dot.diameter() / 2; 57 painter.mbk(centerOffset); 58 59 //Paint the row of squares. 60 int painted = 0; 61 while(painted < squaresToPaint){ 62 paintOneDot(painter,dot); 63 painter.mfd(dot.diameter()); 64 painted = painted + 1; 65 } 66 67 //Make the method invariant with respect to painter position. 68 painter.mbk(centerOffset + dot.diameter()); 69 } 70 71 private void paintOneDot(SPainter painter, SCircle dot){ 72 dot.s2(); 73 painter.setColor(randomColor()); 74 painter.paint(dot); 75 dot.x2(); 76 } 77 78 private static int dotsOnLineCount(double lineLength, double sideLength){ 79 int squares = ( (int)Math.ceil( (lineLength - sideLength) /sideLength) + 1); 80 return squares; 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 }