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