1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 import shapes.SSquare; 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 side = (getNumber("small circle radius")) * 2; 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 SSquare square = new SSquare(side); 23 24 // Paint the squares 25 paintCircleOfSquares(painter, circle, square); 26 27 } 28 29 private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square){ 30 // Position the painter to begin drawing the rows 31 painter.mfd(circle.radius()); 32 painter.tr(); 33 // Paint the circle of squares 34 double moved = 0; 35 while (moved < circle.diameter()) { 36 double chord = chordLength(circle.radius() - moved, circle); 37 int squares = squaresOnLineCount(chord, square.side()); 38 paintRow(painter, square, squares); 39 nextRow(painter, square.side()); 40 moved = moved + square.side(); 41 42 } 43 // Make the method invariant with respect to painter position 44 painter.tl(); 45 painter.mfd(circle.radius()); 46 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 57 // Assumes the painter is at the center of the row to paint, facing right. 58 private void paintRow(SPainter painter, SSquare square, int squaresToPaint){ 59 // Move backward 1/2 of the length we're painting to get ready to paint the row. 60 double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2; 61 painter.mbk(centerOffset); 62 63 // Paint the row of squares. 64 int painted = 0; 65 while (painted < squaresToPaint){ 66 paintOneSquare(painter, square); 67 painter.mfd(square.side()); 68 painted = painted + 1; 69 70 } 71 72 // Make the method invariant with respect to painter position. 73 painter.mbk(centerOffset + square.side()); 74 75 } 76 77 private void paintOneSquare(SPainter painter, SSquare square){ 78 square.s2(); 79 painter.setColor(randomColor()); 80 painter.paint(square.inscribingCircle()); 81 square.x2(); 82 83 } 84 85 private static int squaresOnLineCount(double lineLength, double sideLength){ 86 int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 87 return squares; 88 89 } 90 91 private double chordLength(double yOffset, SCircle circle){ 92 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 93 double chordLength = xLength * 2; 94 return chordLength; 95 96 } 97 98 private static int getNumber(String prompt) { 99 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 100 Scanner scanner = new Scanner(nss); 101 return scanner.nextInt(); 102 103 } 104 105 private static Color randomColor() { 106 Random color = new Random(); 107 int r = color.nextInt(256); 108 int g = color.nextInt(256); 109 int b = color.nextInt(256); 110 return new Color(r,g,b); 111 112 } 113 114 public HirstDots() { 115 paintTheImage(); 116 117 } 118 119 public static void main(String[] args) { 120 SwingUtilities.invokeLater(new Runnable() { 121 public void run() { 122 new HirstDots(); 123 } 124 }); 125 } 126 }