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(){ 14 // Get the input information 15 int radius = getNumber("circle radius"); 16 int rad = getNumber("square side length"); 17 String color = getcolor("color"); 18 // Establish the painter 19 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 20 painter.setBrushWidth(3); 21 SCircle circle = new SCircle(radius); 22 SCircle dots = new SCircle(rad); 23 24 // Paint the squares 25 paintCircleOfSquares(painter, circle, dots, color); 26 } 27 28 private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle dots, String color){ 29 // Position the painter to begin drawing the rows 30 painter.mfd(circle.radius()); 31 painter.tr(); 32 // Paint the circle of squares 33 double moved = 0; 34 while (moved < circle.diameter()) { 35 double chord = chordLength(circle.radius() - moved, circle); 36 int squares = squaresOnLineCount(chord, dots.radius()); 37 paintRow(painter, dots, squares, color); 38 nextRow(painter, dots.radius()); 39 moved = moved + dots.radius(); 40 41 } 42 // Make the method invariant with respect to painter position 43 painter.tl(); 44 painter.mfd(circle.radius()); 45 } 46 47 // Move to the next row 48 private void nextRow(SPainter painter, double rowHeight){ 49 painter.tr(); 50 painter.mfd(rowHeight); 51 painter.tl(); 52 } 53 54 // Assumes the painter is at the center of the row to paint, facing right. 55 private void paintRow(SPainter painter, SCircle dots, int squaresToPaint, String color){ 56 // Move backward 1/2 of the length we're painting to get ready to paint the row. 57 double centerOffset = ( (squaresToPaint * dots.radius()) / 2) - dots.radius()/2; 58 painter.mbk(centerOffset); 59 60 // Paint the row of squares. 61 int painted = 0; 62 while (painted < squaresToPaint){ 63 paintOneSquare(painter, dots, color); 64 painter.mfd(dots.radius()); 65 painted = painted + 1; 66 } 67 68 // Make the method invariant with respect to painter position. 69 painter.mbk(centerOffset + dots.radius()); 70 } 71 72 private void paintOneSquare(SPainter painter, SCircle dots, String color){ 73 dots.s5(); 74 75 if (color.equalsIgnoreCase("blue")) { 76 painter.setColor(Color.blue); 77 78 } else if (color.equalsIgnoreCase("red")) { 79 painter.setColor(Color.red); 80 81 } else if (color.equalsIgnoreCase("green")) { 82 painter.setColor(Color.green); 83 84 } 85 if (color == null) { 86 painter.setColor(Color.black); 87 88 } 89 painter.paint(dots); 90 91 92 93 dots.x5(); 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 private static String getcolor(String prompt) { 113 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 114 Scanner scanner = new Scanner(nss); 115 return scanner.next(); 116 } 117 private static Color randomColor() { 118 Random rgen = new Random(); 119 int r = rgen.nextInt(256); 120 int g = rgen.nextInt(256); 121 int b = rgen.nextInt(256); 122 return new Color(r,g,b); 123 } 124 125 public SimpleDots() { 126 paintTheImage(); 127 } 128 129 public static void main(String[] args) { 130 SwingUtilities.invokeLater(new Runnable() { 131 public void run() { 132 new SimpleDots(); 133 } 134 }); 135 } 136 } 137 138