1 /* 2 * A program to paint, centered on the canvas, a circle of colored dots that are spaced apart. The program prompts 3 * the user for red, green, or blue circles. If they dont choose, black is the color selected. 4 */ 5 6 7 package npw; 8 9 import painter.SPainter; 10 import shapes.SCircle; 11 import shapes.SSquare; 12 import javax.swing.JOptionPane; 13 import java.awt.Color; 14 import javax.swing.*; 15 16 import java.util.Random; 17 import java.util.Scanner; 18 19 public class SimpleDots{ 20 21 private void paintTheImage(){ 22 // Get the input information 23 int radius = getNumber("circle radius"); 24 int radius1 = getNumber("dot radius length"); 25 26 // Establish the painter 27 SPainter painter = new SPainter("SimpleDots", radius*2+50, radius*2+50); 28 painter.setBrushWidth(3); 29 SCircle circle = new SCircle(radius); 30 SCircle dot = new SCircle(radius1); 31 32 33 // Paint the squares 34 paintCircleOfDots(painter, circle, dot); 35 } 36 37 38 private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot){ 39 // Position the painter to begin drawing the rows 40 painter.mfd(circle.radius()); 41 painter.tr(); 42 43 44 // Paint the circle of dots 45 double moved = 0; 46 while (moved < circle.diameter()) { 47 double chord = chordLength(circle.radius() - moved, circle); 48 int dots = DotsOnLineCount(chord, dot.diameter()); 49 paintRow(painter, dot, dots); 50 nextRow(painter, dot.diameter()); 51 moved = moved + dot.diameter(); 52 } 53 // Make the method invariant with respect to painter position 54 painter.tl(); 55 painter.mfd(circle.radius()); 56 57 } 58 59 // Move to the next row 60 private void nextRow(SPainter painter, double rowHeight){ 61 painter.tr(); 62 painter.mfd(rowHeight); 63 painter.tl(); 64 65 66 } 67 68 // Assumes the painter is at the center of the row to paint, facing right. 69 private void paintRow(SPainter painter, SCircle dot, int DotsToPaint){ 70 // Move backward 1/2 of the length we're painting to get ready to paint the row. 71 double centerOffset = ( (DotsToPaint * dot.diameter()) / 2) - dot.diameter()/2; 72 painter.mbk(centerOffset); 73 74 75 // Paint the row of dots 76 int painted = 0; 77 while (painted < DotsToPaint){ 78 paintOneCircle(painter, dot); 79 painter.mfd(dot.diameter()); 80 painted = painted + 1; 81 82 } 83 84 // Make the method invariant with respect to painter position. 85 painter.mbk(centerOffset + dot.diameter()); 86 } 87 88 private void paintOneCircle(SPainter painter, SCircle dot){ 89 dot.shrink(dot.radius() * 0.50); 90 choiceColor(painter); 91 painter.paint(dot); 92 dot.expand(dot.radius()); ///Needed for invariance 93 } 94 95 private static int DotsOnLineCount(double lineLength, double dotDiameter){ 96 int dot = ( (int)Math.ceil( ( lineLength - dotDiameter ) / dotDiameter ) + 1); 97 return dot; 98 } 99 100 private double chordLength(double yOffset, SCircle circle){ 101 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 102 double chordLength = xLength * 2; 103 return chordLength; 104 } 105 106 private static int getNumber(String prompt) { 107 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 108 Scanner scanner = new Scanner(nss); 109 return scanner.nextInt(); 110 } 111 private static String getColor(String prompt) { 112 String nss = JOptionPane.showInputDialog (null, prompt+ "?"); 113 Scanner scanner = new Scanner(nss); 114 return scanner.next(); 115 116 } 117 String inputColor = getColor("dot color(red, green, blue)"); 118 private void choiceColor(SPainter painter) { 119 120 if (inputColor.equalsIgnoreCase("red")) { 121 painter.setColor(Color.RED); 122 } else if (inputColor.equalsIgnoreCase("green")) { 123 painter.setColor(Color.GREEN); 124 } else if (inputColor.equalsIgnoreCase("blue")) { 125 painter.setColor(Color.BLUE); 126 } else { 127 painter.setColor(Color.BLACK); 128 } 129 } 130 131 public SimpleDots() { 132 paintTheImage(); 133 } 134 135 public static void main(String[] args) { 136 SwingUtilities.invokeLater(new Runnable() { 137 public void run() { 138 new SimpleDots(); 139 } 140 }); 141 } 142 } 143