1 /* 2 * A program to paint, centered on the canvas, a circle of single colored dots. 3 */ 4 5 package npw; 6 7 import painter.SPainter; 8 import shapes.SCircle; 9 import shapes.SSquare; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Scanner; 14 15 public class SimpleDots { 16 17 private void paintTheImage(){ 18 // Get the input information 19 int radius = getNumber("circle radius"); 20 int side = getNumber("square side length"); 21 String color = getColor("color name"); 22 23 // Establish the painter 24 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 25 painter.setBrushWidth(3); 26 SCircle circle = new SCircle(radius); 27 SSquare square = new SSquare(side); 28 29 if (color.equalsIgnoreCase("red") ) { 30 painter.setColor(Color.RED); 31 } 32 else if (color.equalsIgnoreCase("blue")) { 33 painter.setColor(Color.BLUE); 34 } 35 else if (color.equalsIgnoreCase("green")) { 36 painter.setColor(Color.GREEN); 37 } 38 else { 39 40 painter.setColor(Color.BLACK); 41 } 42 43 // Paint the squares 44 paintSimpleDots(painter, circle, square); 45 } 46 47 private void paintSimpleDots(SPainter painter, SCircle circle, SSquare square){ 48 // Position the painter to begin drawing the rows 49 painter.mfd(circle.radius()); 50 painter.tr(); 51 // Paint the circle of squares 52 double moved = 0; 53 while (moved < circle.diameter()) { 54 double chord = chordLength(circle.radius() - moved, circle); 55 int squares = squaresOnLineCount(chord, square.side()); 56 paintRow(painter, square, squares); 57 nextRow(painter, square.side()); 58 moved = moved + square.side(); 59 } 60 // Make the method invariant with respect to painter position 61 painter.tl(); 62 painter.mfd(circle.radius()); 63 } 64 65 // Move to the next row 66 private void nextRow(SPainter painter, double rowHeight){ 67 painter.tr(); 68 painter.mfd(rowHeight); 69 painter.tl(); 70 } 71 72 // Assumes the painter is at the center of the row to paint, facing right. 73 private void paintRow(SPainter painter, SSquare square, int squaresToPaint){ 74 // Move backward 1/2 of the length we're painting to get ready to paint the row. 75 double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2; 76 painter.mbk(centerOffset); 77 78 // Paint the row of squares. 79 int painted = 0; 80 while (painted < squaresToPaint){ 81 paintOneSquare(painter, square); 82 painter.mfd(square.side()); 83 painted = painted + 1; 84 } 85 86 // Make the method invariant with respect to painter position. 87 painter.mbk(centerOffset + square.side()); 88 } 89 90 private void paintOneSquare(SPainter painter, SSquare square){ 91 square.s2(); 92 painter.paint(square.inscribingCircle()); 93 //painter.setColor(Color.BLACK); 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 116 private String getColor(String color_name) { 117 String nss = JOptionPane.showInputDialog(null, color_name+"?"); 118 Scanner scanner = new Scanner(nss); 119 return scanner.next(); 120 } 121 122 public SimpleDots() { 123 paintTheImage(); 124 } 125 126 public static void main(String[] args) { 127 SwingUtilities.invokeLater(new Runnable() { 128 public void run() { 129 new SimpleDots(); 130 } 131 }); 132 } 133 }