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