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("Circle Diameter"); 21 double radius2= diameter/2; 22 23 // Establish the painter 24 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 25 painter.setBrushWidth(3); 26 SCircle circle = new SCircle(radius); 27 SCircle circles= new SCircle(radius2); 28 29 // Paint the squares 30 paintCircleOfSquares(painter, circle, circles); 31 } 32 33 private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle circles){ 34 // Position the painter to begin drawing the rows 35 painter.mfd(circle.radius()); 36 painter.tr(); 37 // Paint the circle of squares 38 double moved = 0; 39 while (moved < circle.diameter()) { 40 double chord = chordLength(circle.radius() - moved, circle); 41 System.out.println(chord); 42 int lineCircle = circleOnLineCount(chord, circles.diameter()); 43 System.out.println(lineCircle); 44 paintRow(painter, circles, lineCircle); 45 nextRow(painter, circles.diameter()); 46 moved = moved + circles.diameter(); 47 } 48 // Make the method invariant with respect to painter position 49 painter.tl(); 50 painter.mfd(circle.radius()); 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 // Assumes the painter is at the center of the row to paint, facing right. 61 private void paintRow(SPainter painter, SCircle circles, int circleToPaint){ 62 // Move backward 1/2 of the length we're painting to get ready to paint the row. 63 double centerOffset = ( (circleToPaint * circles.diameter() / 2) - circles.diameter()/2); 64 painter.mbk(centerOffset); 65 66 // Paint the row of squares. 67 int painted = 0; 68 while (painted < circleToPaint){ 69 paintOneCircle(painter,circles); 70 painter.mfd(circles.diameter()); 71 painted = painted + 1; 72 } 73 74 // Make the method invariant with respect to painter position. 75 painter.mbk(centerOffset + circles.diameter()); 76 } 77 78 private void paintOneCircle(SPainter painter, SCircle circles){ 79 painter.setColor(randomColor()); 80 circles.s3(); 81 painter.paint(circles); 82 circles.x3(); 83 84 } 85 86 private static int circleOnLineCount(double lineLength, double sideLength){ 87 System.out.println(lineLength + " " + sideLength); 88 int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 89 return circles; 90 } 91 92 private double chordLength(double yOffset, SCircle circle){ 93 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 94 double chordLength = xLength * 2; 95 return chordLength; 96 } 97 98 private static int getNumber(String prompt) { 99 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 100 Scanner scanner = new Scanner(nss); 101 return scanner.nextInt(); 102 } 103 104 private static Color randomColor() { 105 Random rgen = new Random(); 106 int r = rgen.nextInt(256); 107 int g = rgen.nextInt(256); 108 int b = rgen.nextInt(256); 109 return new Color(r,g,b); 110 } 111 112 public HirstDots() { 113 paintTheImage(); 114 } 115 116 public static void main(String[] args) { 117 SwingUtilities.invokeLater(new Runnable() { 118 public void run() { 119 new HirstDots(); 120 } 121 }); 122 } 123 } 124