1 /* 2 * A program to paint, centered on the canvas, a circle of a red, blue, green, or black set of circles that are spaced out based on users input 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.Scanner; 13 14 public class SimpleDots { 15 16 private void paintTheImage() { 17 // Get the input information 18 int radius = getNumber("circle radius"); 19 int dotradius = getNumber("dotcircle radius"); 20 String dotColor = getColor("color of dot"); 21 22 23 // Establish the painter 24 SPainter painter = new SPainter("SimpleDots", radius * 2 + 50, radius * 2 + 50); 25 painter.setBrushWidth(3); 26 SCircle circle = new SCircle(radius); 27 SCircle dot = new SCircle(dotradius); 28 29 // Paint the squares 30 paintSimpleDots(painter, circle, dot, dotColor); 31 } 32 33 private void paintSimpleDots(SPainter painter, SCircle circle, SCircle dot, String dotColor) { 34 // Position the painter to begin drawing the rows 35 painter.mfd(circle.radius()); 36 painter.tr(); 37 // Paint the circle of squares 38 double moved = 0; 39 while (moved < circle.diameter()) { 40 double chord = chordLength(circle.radius() - moved, circle); 41 int squares = squaresOnLineCount(chord, dot.radius()); 42 paintRow(painter, dot, squares, dotColor); 43 nextRow(painter, dot.radius()); 44 moved = moved + dot.radius(); 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 dot, int squaresToPaint, String dotColor) { 60 // Move backward 1/2 of the length we're painting to get ready to paint the row. 61 double centerOffset = ((squaresToPaint * dot.radius()) / 2) - dot.radius() / 2; 62 painter.mbk(centerOffset); 63 64 // Paint the row of squares. 65 int painted = 0; 66 while (painted < squaresToPaint) { 67 paintOneDot(painter, dot, dotColor); 68 painter.mfd(dot.radius()); 69 painted = painted + 1; 70 } 71 72 // Make the method invariant with respect to painter position. 73 painter.mbk(centerOffset + dot.radius()); 74 } 75 76 private void paintOneDot(SPainter painter, SCircle dot, String dotColor) { 77 // Color dots based on input 78 if (dotColor.equalsIgnoreCase("blue")) { 79 painter.setColor(Color.blue); 80 } else if (dotColor.equalsIgnoreCase("red")){ 81 painter.setColor(Color.red); 82 } else if (dotColor.equalsIgnoreCase("green")) { 83 painter.setColor(Color.green); 84 } else { 85 painter.setColor(Color.black); 86 } 87 88 dot.s2(); 89 dot.s2(); 90 painter.paint(dot); 91 dot.x2(); 92 dot.x2(); 93 94 } 95 96 private static int squaresOnLineCount(double lineLength, double sideLength) { 97 int squares = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1); 98 return squares; 99 } 100 101 private double chordLength(double yOffset, SCircle circle) { 102 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 103 double chordLength = xLength * 2; 104 return chordLength; 105 } 106 107 private static int getNumber(String prompt) { 108 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 109 Scanner scanner = new Scanner(nss); 110 return scanner.nextInt(); 111 } 112 113 private static String getColor(String prompt) { 114 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 115 Scanner scanner = new Scanner(nss); 116 return scanner.next(); 117 } 118 119 public SimpleDots() { 120 paintTheImage(); 121 } 122 123 public static void main(String[] args) { 124 SwingUtilities.invokeLater(new Runnable() { 125 public void run() { 126 new SimpleDots(); 127 } 128 }); 129 } 130 }