1 /* 2 ** A Program to paint, centered in the canvas, a circle of randomly colored, 3 * seperated dots from each other 4 5 */ 6 7 package npw; 8 9 import painter.SPainter; 10 import shapes.SCircle; 11 import shapes.SSquare; 12 13 import javax.swing.*; 14 import java.awt.*; 15 import java.util.Random; 16 import java.util.Scanner; 17 18 public class HirstDots { 19 20 public static void main(String[]args){ 21 SwingUtilities.invokeLater(new Runnable() { 22 @Override 23 public void run() { 24 new HirstDots(); 25 } 26 }); 27 } 28 29 30 private void paintTheImage() { 31 //Get the input information 32 int radius = getNumber("Circle radius"); 33 int side = getNumber("square side length"); 34 35 //Establish the painter 36 SPainter painter = new SPainter("HirstDots", radius * 2 + 50, radius * 2 + 50); 37 38 SCircle circle = new SCircle(radius); 39 SSquare square = new SSquare(side); 40 41 //paint the squares 42 paintCircleOfSquares(painter, circle, square); 43 } 44 45 private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square) { 46 //position the painter to begin drawing the rows 47 painter.mfd(circle.radius()); 48 painter.tr(); 49 //paint the circle of squares 50 double moved = 0; 51 while (moved < circle.diameter()) { 52 double chord = chordLength(circle.radius() - moved, circle); 53 int squares = squareOnLineCount(chord, square.side()); 54 paintRow(painter, square, squares); 55 nextRow(painter, square.side()); 56 moved = moved + square.side(); 57 58 } 59 //Make the method invariant with respect to painter position 60 painter.tl(); 61 painter.mfd(circle.radius()); 62 } 63 64 // Move to the next row 65 private void nextRow(SPainter painter, double rowHeight) { 66 painter.tr(); 67 painter.mfd(rowHeight); 68 painter.tl(); 69 } 70 71 //Assume the painter is at the center of the row to paint, facing right. 72 private void paintRow(SPainter painter, SSquare square, int squaresToPaint) { 73 //move backward 1/2 of the length we're painting to get ready to paint the row. 74 double centerOfset = ((squaresToPaint * square.side()) / 2) - square.side() / 2; 75 painter.mbk(centerOfset); 76 77 //paint the row of squares. 78 int painted = 0; 79 while (painted < squaresToPaint) { 80 paintOneSquare(painter, square); 81 painter.mfd(square.side()); 82 painted = painted + 1; 83 } 84 85 //Make the method invariant with respect to painter position.Deterministic Invention 86 painter.mbk(centerOfset + square.side()); 87 } 88 private void paintOneSquare(SPainter painter, SSquare square){ 89 90 square.s2(); 91 SCircle circle= square.inscribingCircle(); 92 painter.setColor(randomColor()); 93 painter.paint(circle); 94 square.expand(square.side()); 95 96 97 } 98 99 private static int squareOnLineCount(double lineLength, double sideLenth){ 100 int squares= ((int) Math.ceil((lineLength-sideLenth)/sideLenth)+1); 101 return squares; 102 } 103 private double chordLength(double yOffset, SCircle circle){ 104 double xLength= Math.sqrt(Math.pow(circle.radius(), 2)- Math.pow(yOffset, 2)); 105 double chordLength= xLength*2; 106 return chordLength; 107 } 108 private static int getNumber(String prompt){ 109 String nss= JOptionPane.showInputDialog(null, prompt+ "?"); 110 Scanner scanner= new Scanner(nss); 111 return scanner.nextInt(); 112 } 113 private static Color randomColor(){ 114 Random rgen= new Random(); 115 int r= rgen.nextInt(256); 116 int g= rgen.nextInt(256); 117 int b= rgen.nextInt(256); 118 return new Color(r,g,b); 119 } 120 public HirstDots(){ 121 paintTheImage(); 122 } 123 124 } 125 126 127 128 129