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