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