1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.util.Random; 9 import java.util.Scanner; 10 11 public class SimpleDots { 12 13 private void paintTheImage() {// Get the input information 14 int radius = getNumber("circle radius"); 15 int dotRadius = getNumber("radius of dot"); 16 String input = getColor("Inputs Are: "); 17 18 // Establish the painter 19 SPainter painter = new SPainter("Simple Dots", radius * 2 + 50, radius * 2 + 50); 20 painter.setBrushWidth(3); 21 SCircle circle = new SCircle(radius); 22 SCircle dot = new SCircle(dotRadius); 23 24 // Paint the squares 25 paintCircleOfSquares(painter, circle, dot, input); 26 } 27 28 private String getColor(String prompt) { 29 String rbg = JOptionPane.showInputDialog(null, prompt + "blue | green | red | other"); 30 return rbg; 31 } 32 33 private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle dot, String input) { 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 dots = circlesOnLineCount(chord, dot.diameter()); 42 paintRow(painter, dot,dots,input); 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 CirclesToPaint, String input) { 60 // Move backward 1/2 of the length we're painting to get ready to paint the row. 61 double centerOffset = ((CirclesToPaint * dot.diameter()) / 2) - dot.diameter() / 2; 62 painter.mbk(centerOffset); 63 64 // Paint the row of squares. 65 int painted = 0; 66 while (painted < CirclesToPaint) { 67 paintOneSquare(painter, dot,input); 68 painter.mfd(dot.diameter()); 69 painted = painted + 1; 70 } 71 72 // Make the method invariant with respect to painter position. 73 painter.mbk(centerOffset + dot.diameter()); 74 } 75 76 private void paintOneSquare(SPainter painter, SCircle dot, String input) { 77 dot.s2(); 78 if (input.equals("red")){ 79 painter.setColor(Color.RED); 80 painter.paint(dot);} 81 else if (input.equals("green")){ 82 painter.setColor(Color.GREEN); 83 painter.paint(dot); 84 } else if (input.equals("blue")) { 85 painter.setColor(Color.BLUE); 86 painter.paint(dot); 87 } 88 dot.x2(); 89 90 } 91 92 private static int circlesOnLineCount(double lineLength, double sideLength) { 93 int circles = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1); 94 return circles; 95 } 96 97 private double chordLength(double yOffset, SCircle circle) { 98 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 99 double chordLength = xLength * 2; 100 return chordLength; 101 } 102 103 private static int getNumber(String prompt) { 104 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 105 Scanner scanner = new Scanner(nss); 106 return scanner.nextInt(); 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 }