1 /* 2 * Program to paint, centered on the canvas, a circle of randomly colored,circles with spaces inbetween them sort of 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 private void paintTheImage(){ 17 int radius = getNumber("circle radius"); 18 int rad = getNumber("dot diameter length"); 19 20 SPainter painter = new SPainter("Circle of Dots", radius*2+50, radius*2+50); 21 painter.setBrushWidth(3); 22 SCircle circle = new SCircle(radius); 23 SCircle dot = new SCircle(rad); 24 25 paintCircleOfDots(painter,circle,dot); 26 } 27 28 private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot) { 29 painter.mfd(circle.radius()); 30 painter.tr(); 31 32 double moved = 0; 33 while ( moved < circle.diameter()) { 34 double chord = chordLength(circle.radius() - moved, circle); 35 int dotz = dotsOnLineCount(chord, dot.radius()); 36 paintRow(painter,dot,dotz); 37 nextRow(painter, dot.radius()); 38 moved = moved + dot.radius(); 39 } 40 painter.tl(); 41 painter.mfd(circle.radius()); 42 } 43 44 45 46 47 private void nextRow(SPainter painter, double rowHeight) { 48 painter.tr(); 49 painter.mfd(rowHeight); 50 painter.tl(); 51 } 52 private void paintRow(SPainter painter, SCircle dot, int dotsToPaint) { 53 double centerOffset = ( ( dotsToPaint * dot.radius()) / 2) - dot.radius()/2; 54 painter.mbk(centerOffset); 55 56 int painted = 0; 57 while (painted < dotsToPaint){ 58 paintOneSquare(painter, dot); 59 painter.mfd(dot.radius()); 60 painted = painted +1; 61 } 62 63 painter.mbk(centerOffset + dot.radius()); 64 } 65 66 private void paintOneSquare(SPainter painter, SCircle dot) { 67 dot.s3(); 68 painter.setColor(randomColor()); 69 painter.paint(dot); 70 //painter.setColor(Color.BLACK); 71 //painter.draw(dot); 72 dot.x3(); 73 74 75 } 76 77 78 private int dotsOnLineCount(double lineLength, double sideLength) { 79 int circles = ( (int) Math.ceil( (lineLength-sideLength )/ sideLength) + 1); 80 return circles; 81 } 82 private double chordLength(double yOffSet, SCircle circle) { 83 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffSet, 2)); 84 double chordLength = xLength * 2; 85 return chordLength; 86 } 87 private int getNumber(String prompt ) { 88 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 89 Scanner scanner = new Scanner(nss); 90 return scanner.nextInt(); 91 } 92 private Color randomColor() { 93 Random rgen = new Random(); 94 int r = rgen.nextInt(256); 95 int g = rgen.nextInt(256); 96 int b = rgen.nextInt(256); 97 return new Color(r,g,b); 98 } 99 public HirstDots() { 100 paintTheImage(); 101 } 102 103 public static void main(String[] args) { 104 SwingUtilities.invokeLater(new Runnable() { 105 public void run() { 106 new HirstDots(); 107 108 } 109 }); 110 } 111 } 112