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