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 static String String; 14 15 private void paintTheImage() { 16 // Get the input information 17 int radius = getNumber("circle radius"); 18 int side = getNumber("Inside circles"); 19 String color = getColor("Color Name"); 20 21 // Establish the painter 22 SPainter painter = new SPainter("Circle of Circles", radius * 2 + 50, radius * 2 + 50); 23 painter.setBrushWidth(3); 24 SCircle circle = new SCircle(radius); 25 SCircle square = new SCircle(side); 26 27 if (color.equalsIgnoreCase("red")) { 28 painter.setColor(Color.red); 29 } else if ((color.equalsIgnoreCase("blue"))){ 30 painter.setColor(Color.blue); 31 } else if (color.equalsIgnoreCase("green")){ 32 painter.setColor(Color.green); 33 } else { 34 painter.setColor(Color.black); 35 } 36 37 // Paint the squares 38 paintCircleOfSquares(painter, circle, square); 39 } 40 41 private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle square) { 42 // Position the painter to begin drawing the rows 43 painter.mfd(circle.radius()); 44 painter.tr(); 45 // Paint the circle of squares 46 double moved = 0; 47 while (moved < circle.diameter()) { 48 double chord = chordLength(circle.radius() - moved, circle); 49 int squares = squaresOnLineCount(chord, square.diameter()); 50 paintRow(painter, square, squares); 51 nextRow(painter, square.diameter()); 52 moved = moved + square.diameter(); 53 } 54 // Make the method invariant with respect to painter position 55 painter.tl(); 56 painter.mfd(circle.radius()); 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 // Assumes the painter is at the center of the row to paint, facing right. 67 private void paintRow(SPainter painter, SCircle square, int squaresToPaint) { 68 // Move backward 1/2 of the length we're painting to get ready to paint the row. 69 double centerOffset = ((squaresToPaint * square.diameter()) / 2) - square.diameter() / 2; 70 painter.mbk(centerOffset); 71 72 // Paint the row of squares. 73 int painted = 0; 74 while (painted < squaresToPaint) { 75 paintOneSquare(painter, square); 76 painter.mfd(square.diameter()); 77 painted = painted + 1; 78 } 79 80 // Make the method invariant with respect to painter position. 81 painter.mbk(centerOffset + square.diameter()); 82 } 83 84 private void paintOneSquare(SPainter painter, SCircle square) { 85 square.s2(); 86 painter.paint(square); 87 square.x2(); 88 } 89 90 private static int squaresOnLineCount(double lineLength, double sideLength) { 91 int squares = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1); 92 return squares; 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 private static String getColor(String prompt) { 107 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 108 Scanner scanner = new Scanner(nss); 109 return scanner.nextLine(); 110 } 111 112 public SimpleDots() { 113 paintTheImage(); 114 } 115 116 public static void main(String[] args) { 117 SwingUtilities.invokeLater(new Runnable() { 118 public void run() { 119 new SimpleDots(); 120 } 121 }); 122 } 123 } 124