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