1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, unframed dots. 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 String color = getString("Type in red, blue, or green"); 20 Color userColor = paintColor(color); 21 int dotradius = getNumber("Small circle radius"); 22 23 // Establish the painter 24 SPainter painter = new SPainter("Simple Dots", radius*2+50, radius*2+50); 25 painter.setBrushWidth(3); 26 SCircle circle = new SCircle(radius); 27 SCircle dots = new SCircle(dotradius); 28 painter.setColor(userColor); 29 30 31 // Paint the circles 32 paintSimpleDots(painter, circle, dots); 33 } 34 35 private void paintSimpleDots(SPainter painter, SCircle circle, SCircle dot){ 36 // Position the painter to begin drawing the rows 37 painter.mfd(circle.radius()); 38 painter.tr(); 39 // Paint the simple dots 40 double moved = 0; 41 while (moved < circle.diameter()) { 42 System.out.println(moved + " " + circle.diameter()); 43 double chord = chordLength(circle.radius() - moved, circle); 44 int dots = dotsOnLineCount(chord, dot.diameter()); 45 paintRow(painter, dot, dots); 46 nextRow(painter, dot.diameter()); 47 //moved = moved + circle.diameter(); 48 moved += dot.diameter(); 49 } 50 // Make the method invariant with respect to painter position 51 painter.tl(); 52 painter.mfd(circle.radius()); 53 } 54 55 // Move to the next row 56 private void nextRow(SPainter painter, double rowHeight){ 57 painter.tr(); 58 painter.mfd(rowHeight); 59 painter.tl(); 60 } 61 62 // Assumes the painter is at the center of the row to paint, facing right. 63 private void paintRow(SPainter painter, SCircle circle, int dotsToPaint){ 64 // Move backward 1/2 of the length we're painting to get ready to paint the row. 65 double centerOffset = ( (dotsToPaint * circle.diameter()) / 2) - circle.diameter()/2; 66 painter.mbk(centerOffset); 67 68 // Paint the row of squares. 69 int painted = 0; 70 while (painted < dotsToPaint){ 71 System.out.println(painted); 72 paintOneSquare(painter, circle); 73 painter.mfd(circle.diameter()); 74 painted = painted + 1; 75 } 76 77 // Make the method invariant with respect to painter position. 78 painter.mbk(centerOffset + circle.diameter()); 79 } 80 81 82 83 private void paintOneSquare(SPainter painter, SCircle circle){ 84 circle.s2(); 85 System.out.println("here"); 86 painter.paint(circle); 87 circle.x2(); 88 } 89 90 private static int dotsOnLineCount(double lineLength, double sideLength){ 91 int dots = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 92 return dots; 93 } 94 95 private double chordLength(double yOffset, SCircle circle){ 96 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 97 double chordLength = xLength * 2; 98 return chordLength; 99 } 100 101 private static int getNumber(String prompt) { 102 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 103 Scanner scanner = new Scanner(nss); 104 return scanner.nextInt(); 105 } 106 107 public static Color paintColor(String colorName) { 108 int r; 109 int b; 110 int g; 111 if (colorName.equals("red")) { 112 r = 255; 113 b = 0; 114 g = 0; 115 } else if (colorName.equals("blue")) { 116 r = 0; 117 g = 0; 118 b = 255; 119 } else if (colorName.equals("green")) { 120 r = 0; 121 g = 204; 122 b = 0; 123 124 } else { 125 r = 0; 126 b = 0; 127 g = 0; 128 } 129 return new Color(r, g, b); 130 } 131 132 public SimpleDots() { 133 paintTheImage(); 134 } 135 136 private static String getString(String prompt) { 137 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 138 Scanner scanner = new Scanner(nss); 139 return scanner.next(); 140 } 141 142 public static void main(String[] args) { 143 SwingUtilities.invokeLater(new Runnable() { 144 public void run() { 145 new SimpleDots(); 146 } 147 }); 148 } 149 } 150 151 152