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