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 import java.awt.Color; 12 13 14 15 public class SimpleDots { 16 17 private void paintTheImage() { 18 //Get the input info 19 int radius = getNumber("circle radius"); 20 int diameter = getNumber("dot radius"); 21 Color rgb = setTheColor ("Red, Blue, or Green"); 22 23 //Establish the painter 24 SPainter painter = new SPainter("Simple Dots", radius * 2 + 50, radius * 2 + 50); 25 painter.setBrushWidth(3); 26 painter.setColor(rgb); 27 SCircle circle = new SCircle(radius); 28 SCircle dot = new SCircle(diameter / 2); 29 30 31 //Paint the dots 32 paintCircleOfDots(painter, circle, dot); 33 } 34 35 private Color setTheColor(String rgb) { 36 //creating the color for the dots (red or blue or green if else then black 37 Color color = new Color(0, 0, 0); 38 String setTheColor = JOptionPane.showInputDialog(null, rgb); 39 if (setTheColor == null) { 40 color = Color.BLACK; 41 } else if (setTheColor.equalsIgnoreCase("Red")) { 42 color = Color.RED; 43 } else if (setTheColor.equalsIgnoreCase("Blue")) { 44 color = Color.BLUE; 45 } else if (setTheColor.equalsIgnoreCase("Green")) { 46 color = Color.GREEN; 47 } 48 return color; 49 } 50 51 52 private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot) { 53 //Position the painter to begin drawing the rows 54 painter.mfd(circle.radius()); 55 painter.tr(); 56 // Paint the circle of dots 57 double moved = 0; 58 while (moved < circle.diameter()) { 59 double chord = chordLength(circle.radius() - moved, circle); 60 int dots = dotOnLineCount(chord, dot.diameter() / 2); 61 paintRow(painter, dot, dots); 62 nextRow(painter, (dot.diameter() / 2)); 63 moved = moved + (dot.diameter() / 2); 64 } 65 66 //Make the method invariant with respect to painter position 67 painter.tl(); 68 painter.mfd(circle.radius()); 69 } 70 71 //Move to the next row 72 private void nextRow(SPainter painter, double rowHeight) { 73 painter.tr(); 74 painter.mfd(rowHeight); 75 painter.tl(); 76 } 77 78 //Assumes the painter is at the center of the row to paint, facing right. 79 private void paintRow(SPainter painter, SCircle dot, int dotsToPaint) { 80 //Move backward 1/2 of the length we're painting to get ready to paint the row. 81 double centerOffset = ((dotsToPaint * dot.diameter() / 4) - dot.diameter() / 4); 82 painter.mbk(centerOffset); 83 84 //Paint the row of dots. 85 int painted = 0; 86 while (painted < dotsToPaint) { 87 paintOneDot(painter, dot); 88 painter.mfd(dot.diameter() / 2); 89 painted = painted + 1; 90 } 91 92 //Make the method invariant with respect to painter position. 93 painter.mbk(centerOffset + (dot.diameter() / 2)); 94 } 95 96 private void paintOneDot(SPainter painter, SCircle dot) { 97 dot.s3(); 98 painter.paint(dot); 99 dot.x3(); 100 } 101 102 private static int dotOnLineCount(double lineLength, double diameterLength) { 103 int dots = ((int) Math.ceil((lineLength - diameterLength) / diameterLength) + 1); 104 return dots; 105 } 106 107 private double chordLength(double yOffset, SCircle circle) { 108 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 109 double chordLength = xLength * 2; 110 return chordLength; 111 } 112 113 private static int getNumber(String prompt) { 114 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 115 Scanner scanner = new Scanner(nss); 116 return scanner.nextInt(); 117 } 118 119 public SimpleDots() { 120 paintTheImage(); 121 } 122 123 public static void main (String[]args){ 124 SwingUtilities.invokeLater(new Runnable() { 125 @Override 126 public void run() { 127 new SimpleDots(); 128 } 129 }); 130 } 131 } 132 133