1 package npw; 2 import painter.SPainter; 3 import shapes.SCircle; 4 import javax.swing.*; 5 import java.awt.*; 6 import java.util.Random; 7 import java.util.Scanner; 8 public class HirstDots 9 { 10 private void paintTheImage() 11 { 12 int radius = getNumber("circle radius"); 13 int altRadius = getNumber("small circle radius"); 14 SPainter painter = new SPainter("Hirst Dots", radius*2+50, radius*2+50); 15 painter.setBrushWidth(3); 16 SCircle circle = new SCircle(radius); 17 SCircle altCircle = new SCircle(altRadius); 18 paintHirstSquares(painter, circle, altCircle); 19 } 20 private void paintHirstSquares(SPainter painter, SCircle circle, SCircle altCircle) 21 { 22 painter.mfd(circle.radius()); 23 painter.tr(); 24 double moved = 0; 25 while (moved < circle.diameter()) 26 { 27 double chord = chordLength(circle.radius() - moved, circle); 28 int circles = circlesOnLineCount(chord, altCircle.radius()); 29 paintRow(painter, altCircle, circles); 30 nextRow(painter, altCircle.radius()); 31 moved = moved + altCircle.radius(); 32 } 33 painter.tl(); 34 painter.mfd(circle.radius()); 35 } 36 private void nextRow(SPainter painter, double rowHeight){ 37 painter.tr(); 38 painter.mfd(rowHeight); 39 painter.tl(); 40 } 41 private void paintRow(SPainter painter, SCircle altCircle, int circlesToPaint) 42 { 43 double centerOffset = ( (circlesToPaint * altCircle.radius()) / 2) - altCircle.radius()/2; 44 painter.mbk(centerOffset); 45 int painted = 0; 46 while (painted < circlesToPaint) 47 { 48 paintOneSquare(painter, altCircle); 49 painter.mfd(altCircle.radius()); 50 painted = painted + 1; 51 } 52 painter.mbk(centerOffset + altCircle.radius()); 53 } 54 private void paintOneSquare(SPainter painter, SCircle altCircle) 55 { 56 painter.setColor(randomColor()); 57 altCircle.s3(); 58 painter.paint(altCircle); 59 altCircle.x3(); 60 } 61 private static int circlesOnLineCount(double lineLength, double sideLength) 62 { 63 int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 64 return circles; 65 } 66 private double chordLength(double yOffset, SCircle circle) 67 { 68 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 69 double chordLength = xLength * 2; 70 return chordLength; 71 } 72 private static int getNumber(String prompt) 73 { 74 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 75 Scanner scanner = new Scanner(nss); 76 return scanner.nextInt(); 77 } 78 private static Color randomColor() 79 { 80 Random rgen = new Random(); 81 int r = rgen.nextInt(256); 82 int g = rgen.nextInt(256); 83 int b = rgen.nextInt(256); 84 return new Color(r,g,b); 85 } 86 public HirstDots() 87 { 88 paintTheImage(); 89 } 90 public static void main(String[] args) 91 { 92 SwingUtilities.invokeLater(new Runnable() 93 { 94 public void run() 95 { 96 new HirstDots(); 97 } 98 }); 99 } 100 } 101