1 /* 2 A program to paint, centered on a canvas, a circle of colored circles spaced from each other. 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.Random; 14 import java.util.Scanner; 15 16 public class SimpleDots{ 17 18 private void paintTheImage(){ 19 //Get input information 20 int radius = getNumber("large circle radius"); 21 int side = getNumber("small circles radius"); 22 String color = getString("green, blue, or red"); 23 24 //Establish the painter 25 SPainter painter = new SPainter("Simple Dots",((radius*2)+50),((radius*2)+50)); 26 painter.setBrushWidth(3); 27 SCircle circle = new SCircle(radius); 28 SCircle hirst = new SCircle(side); 29 30 //Paint the squares 31 paintCircleOfSquares(color, painter,circle,hirst); 32 } 33 34 private void paintCircleOfSquares(String color, SPainter painter, SCircle circle, SCircle hirst){ 35 //Position the painter to begin drawing the rows 36 painter.mfd(circle.radius()); 37 painter.tr(); 38 //Paint the circle of squares 39 double moved=0; 40 while (moved < circle.diameter()) { 41 double chord = chordLength(circle.radius()-moved, circle); 42 int squares = squaresOnLineCount(chord, hirst.diameter()); 43 paintRow(color, painter, hirst, squares); 44 nextRow(painter, hirst.diameter()); 45 moved=moved+hirst.diameter(); 46 } 47 //Make the method invariant with respect to painter position 48 painter.tl(); 49 painter.mfd(circle.radius()); 50 } 51 52 //Move to the next row 53 private void nextRow(SPainter painter, double rowHeight){ 54 painter.tr(); 55 painter.mfd(rowHeight); 56 painter.tl(); 57 } 58 59 //Assumes the painter is at the center of the row to paint, facing right 60 private void paintRow(String color, SPainter painter, SCircle hirst, int squaresToPaint){ 61 //Move backward 1/2 of the length we're painting to get ready to paint the row 62 double centerOffset = (((squaresToPaint*hirst.diameter())/2)-hirst.diameter()/2); 63 painter.mbk(centerOffset); 64 65 //Paint the row of squares 66 int painted=0; 67 while (painted < squaresToPaint) { 68 paintOneSquare(color,painter,hirst); 69 painter.mfd(hirst.diameter()); 70 painted=painted+1; 71 } 72 73 //make the method invariant with respect to painter position 74 painter.mbk(centerOffset+hirst.diameter()); 75 } 76 77 private void paintOneSquare(String color, SPainter painter, SCircle hirst){ 78 hirst.s2(); 79 painter.setColor(color(color)); 80 painter.paint(hirst); 81 hirst.x2(); 82 } 83 84 private static int squaresOnLineCount(double lineLength, double sideLength){ 85 int hirsts = ((int)Math.ceil((lineLength-sideLength)/sideLength)+1); 86 return hirsts; 87 } 88 89 private double chordLength(double yOffset, SCircle circle){ 90 double xLength=Math.sqrt(Math.pow(circle.radius(),2)-Math.pow(yOffset,2)); 91 double chordLength=xLength*2; 92 return chordLength; 93 } 94 95 private static int getNumber(String prompt) { 96 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 97 Scanner scanner = new Scanner(nss); 98 return scanner.nextInt(); 99 } 100 101 private static String getString(String prompt) { 102 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 103 Scanner scanner = new Scanner(nss); 104 return scanner.next(); 105 } 106 107 private static Color color(String color) { 108 if (color.equalsIgnoreCase("green")) { 109 return Color.GREEN; 110 } else if (color.equalsIgnoreCase("blue")) { 111 return Color.BLUE; 112 } else if (color.equalsIgnoreCase("red")) { 113 return Color.RED; 114 } else { 115 return Color.BLACK; 116 } 117 } 118 119 public SimpleDots(){ 120 paintTheImage(); 121 } 122 123 public static void main(String[] args) { 124 SwingUtilities.invokeLater(new Runnable() { 125 public void run() { 126 new SimpleDots(); 127 } 128 }); 129 } 130 }