1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 3 */ 4 package npw; 5 6 import painter.SPainter; 7 import shapes.SCircle; 8 9 import javax.swing.*; 10 import java.awt.*; 11 import java.util.Scanner; 12 13 public class SimpleDots { 14 15 private void paintTheImage(){ 16 // Get the input information 17 int radius = getNumber("circle radius"); 18 int altRadius = getNumber("small circle radius"); 19 20 // Establish the painter 21 SPainter painter = new SPainter("Simple Dots", radius*2+50, radius*2+50); 22 painter.setBrushWidth(3); 23 SCircle circle = new SCircle(radius); 24 SCircle altCircle = new SCircle(altRadius); 25 Color Color = getColor(); 26 27 // Paint the circles 28 paintHirstCircles(painter, circle, altCircle,Color); 29 } 30 31 private void paintHirstCircles(SPainter painter, SCircle circle, SCircle altCircle,Color Color){ 32 // Position the painter to begin drawing the rows 33 painter.mfd(circle.radius()); 34 painter.tr(); 35 // Paint the circle of squares 36 double moved = 0; 37 while (moved < circle.diameter()) { 38 double chord = chordLength(circle.radius() - moved, circle); 39 int circles = circlesOnLineCount(chord, altCircle.radius()); 40 paintRow(painter, altCircle, circles,Color); 41 nextRow(painter, altCircle.radius()); 42 moved = moved + altCircle.radius(); 43 } 44 // Make the method invariant with respect to painter position 45 painter.tl(); 46 painter.mfd(circle.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 altCircle, int circlesToPaint,Color Color){ 58 // Move backward 1/2 of the length we're painting to get ready to paint the row. 59 double centerOffset = ( (circlesToPaint * altCircle.radius()) / 2) - altCircle.radius()/2; 60 painter.mbk(centerOffset); 61 62 // Paint the row of squares. 63 int painted = 0; 64 while (painted < circlesToPaint){ 65 painter.setColor(Color); 66 altCircle.s3(); 67 painter.paint(altCircle); 68 altCircle.x3(); 69 painter.mfd(altCircle.radius()); 70 painted = painted + 1; 71 } 72 73 // Make the method invariant with respect to painter position. 74 painter.mbk(centerOffset + altCircle.radius()); 75 } 76 77 private void paintOneCircle(SPainter painter, SCircle altCircle) { 78 painter.setColor(getColor()); 79 altCircle.s3(); 80 painter.paint(altCircle); 81 altCircle.x3(); 82 } 83 84 85 private static int circlesOnLineCount(double lineLength, double sideLength){ 86 int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 87 return circles; 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 private 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 private static Color getColor() { 103 String color = JOptionPane.showInputDialog(null,"color?"); 104 if (color.equalsIgnoreCase("blue")) { 105 return Color.BLUE; 106 } else if (color.equalsIgnoreCase("red")) { 107 return Color.RED; 108 } else if (color.equalsIgnoreCase("green")) { 109 return Color.GREEN; 110 } else { 111 return Color.BLACK; 112 } 113 } 114 115 116 public SimpleDots() { 117 paintTheImage(); 118 } 119 120 public static void main(String[] args) { 121 SwingUtilities.invokeLater(new Runnable() { 122 public void run() { 123 new SimpleDots(); 124 } 125 }); 126 } 127 } 128