1 /* 2 This program displays a circle of dots 3 */ 4 package npw; 5 6 import painter.SPainter; 7 import shapes.SCircle; 8 import shapes.SSquare; 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 radius1 = getNumber("dot radius length"); 21 22 // Establish the painter 23 SPainter painter = new SPainter("HirstDots", radius*2+50, radius*2+50); 24 painter.setBrushWidth(3); 25 SCircle circle = new SCircle(radius); 26 SCircle dot = new SCircle(radius1); 27 28 // Paint the squares 29 paintCircleOfDots(painter, circle, dot); 30 } 31 32 private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot){ 33 // Position the painter to begin drawing the rows 34 painter.mfd(circle.radius()); 35 painter.tr(); 36 37 38 // Paint the circle of dots 39 double moved = 0; 40 while (moved < circle.diameter()) { 41 double chord = chordLength(circle.radius() - moved, circle); 42 int dots = DotsOnLineCount(chord, dot.diameter()); 43 paintRow(painter, dot, dots); 44 nextRow(painter, dot.diameter()); 45 moved = moved + dot.diameter(); 46 } 47 // Make the method invariant with respect to painter position 48 painter.tl(); 49 painter.mfd(circle.radius()); 50 51 } 52 53 // Move to the next row 54 private void nextRow(SPainter painter, double rowHeight){ 55 painter.tr(); 56 painter.mfd(rowHeight); 57 painter.tl(); 58 59 60 } 61 62 // Assumes the painter is at the center of the row to paint, facing right. 63 private void paintRow(SPainter painter, SCircle dot, int DotsToPaint){ 64 // Move backward 1/2 of the length we're painting to get ready to paint the row. 65 double centerOffset = ( (DotsToPaint * dot.diameter()) / 2) - dot.diameter()/2; 66 painter.mbk(centerOffset); 67 68 69 // Paint the row of dots 70 int painted = 0; 71 while (painted < DotsToPaint){ 72 paintOneCircle(painter, dot); 73 painter.mfd(dot.diameter()); 74 painted = painted + 1; 75 76 } 77 78 // Make the method invariant with respect to painter position. 79 painter.mbk(centerOffset + dot.diameter()); 80 } 81 82 private void paintOneCircle(SPainter painter, SCircle dot){ 83 dot.shrink(dot.radius() * 0.50); 84 painter.setColor(randomColor()); 85 painter.paint(dot); 86 dot.expand(dot.radius()); ///Needed for invariance 87 } 88 89 private static int DotsOnLineCount(double lineLength, double dotDiameter){ 90 int dot = ( (int)Math.ceil( ( lineLength - dotDiameter ) / dotDiameter ) + 1); 91 return dot; 92 } 93 94 private double chordLength(double yOffset, SCircle circle){ 95 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 96 double chordLength = xLength * 2; 97 return chordLength; 98 } 99 100 private static int getNumber(String prompt) { 101 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 102 Scanner scanner = new Scanner(nss); 103 return scanner.nextInt(); 104 } 105 106 private static Color randomColor() { 107 Random rgen = new Random(); 108 int r = rgen.nextInt(256); 109 int g = rgen.nextInt(256); 110 int b = rgen.nextInt(256); 111 return new Color(r,g,b); 112 } 113 114 public HirstDots() { 115 paintTheImage(); 116 } 117 118 public static void main(String[] args) { 119 SwingUtilities.invokeLater(new Runnable() { 120 public void run() { 121 new HirstDots(); 122 } 123 }); 124 } 125 } 126