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