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