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