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 SimpleDots { 16 int color = getNumber("1 = blue | 2 = red | 3 = green | 4 = black"); 17 private void paintTheImage(){ 18 // Get the input information 19 int radius = getNumber("circle radius"); 20 int dotsradius = getNumber("dots radius"); 21 22 // Establish the painter 23 SPainter painter = new SPainter("Circle of dots", radius*2+50, radius*2+50); 24 painter.setBrushWidth(3); 25 SCircle circle = new SCircle(radius); 26 SCircle dots = new SCircle(dotsradius); 27 28 // Paint the squares 29 paintHirstSquares(painter, circle, dots); 30 } 31 32 private void paintHirstSquares(SPainter painter, SCircle circle, SCircle dots){ 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 dotsnum = squaresOnLineCount(chord, dots.radius()); 41 paintRow(painter, dots, dotsnum); 42 nextRow(painter, dots.radius()); 43 moved = moved + dots.radius(); 44 45 } 46 // Make the method invariant with respect to painter position 47 painter.tl(); 48 painter.mfd(circle.radius()); 49 } 50 51 // Move to the next row 52 private void nextRow(SPainter painter, double rowHeight){ 53 painter.tr(); 54 painter.mfd(rowHeight); 55 painter.tl(); 56 } 57 58 // Assumes the painter is at the center of the row to paint, facing right. 59 private void paintRow(SPainter painter, SCircle dots, int squaresToPaint){ 60 // Move backward 1/2 of the length we're painting to get ready to paint the row. 61 double centerOffset = ( (squaresToPaint * dots.radius()) / 2) - dots.radius()/2; 62 painter.mbk(centerOffset); 63 64 // Paint the row of squares. 65 int painted = 0; 66 while (painted < squaresToPaint){ 67 paintOneCircle(painter, dots); 68 painter.mfd(dots.radius()); 69 painted = painted + 1; 70 } 71 72 // Make the method invariant with respect to painter position. 73 painter.mbk(centerOffset + dots.radius()); 74 } 75 76 private void paintOneCircle(SPainter painter, SCircle dots){ 77 dots.s3(); 78 if (color == 1) { 79 painter.setColor(Color.blue); 80 } 81 if (color == 2) { 82 painter.setColor(Color.red); 83 } 84 if (color == 3) { 85 painter.setColor(Color.green); 86 } 87 if (color == 4) { 88 painter.setColor(Color.black); 89 } 90 painter.paint(dots); 91 painter.draw(dots); 92 dots.x3(); 93 } 94 95 96 97 private static int squaresOnLineCount(double lineLength, double sideLength){ 98 int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 99 return squares; 100 } 101 102 private double chordLength(double yOffset, SCircle circle){ 103 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 104 double chordLength = xLength * 2; 105 return chordLength; 106 } 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 114 private static Color randomColor() { 115 Random color = new Random(); 116 int r = color.nextInt(256); 117 int g = color.nextInt(256); 118 int b = color.nextInt(256); 119 return new Color(r,g,b); 120 } 121 122 public SimpleDots() { 123 paintTheImage(); 124 } 125 126 public static void main(String[] args) { 127 SwingUtilities.invokeLater(new Runnable() { 128 public void run() { 129 new SimpleDots(); 130 } 131 }); 132 } 133 } 134 135