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