1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 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 the input information 20 int radius = getNumber("circle radius"); 21 int side = getNumber("circle radius length"); 22 Color color = getColor("color (red, blue, or green)"); 23 24 // Establish the painter 25 SPainter painter = new SPainter("Hirst Dots", radius*2+50, radius*2+50); 26 painter.setColor(color); 27 painter.setBrushWidth(3); 28 SCircle circle = new SCircle(radius); 29 SCircle dot = new SCircle(side); 30 31 // Paint the dots 32 paintCircleOfDots(painter, circle, dot); 33 } 34 35 private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot){ 36 // Position the painter to begin drawing the rows 37 painter.mfd(circle.radius()); 38 painter.tr(); 39 // Paint the circle of dots 40 double moved = 0; 41 while (moved < circle.diameter()) { 42 double chord = chordLength(circle.radius() - moved, circle); 43 int dots = dotsOnLineCount(chord, dot.radius()); 44 paintRow(painter, dot, dots); 45 nextRow(painter, dot.radius()); 46 moved = moved + dot.radius(); 47 } 48 // Make the method invariant with respect to painter position 49 painter.tl(); 50 painter.mfd(circle.radius()); 51 } 52 53 // Move to the next row 54 private void nextRow(SPainter painter, double rowHeight){ 55 painter.tr(); 56 painter.mfd(rowHeight); 57 painter.tl(); 58 } 59 60 // Assumes the painter is at the center of the row to paint, facing right. 61 private void paintRow(SPainter painter, SCircle dot, int dotsToPaint){ 62 // Move backward 1/2 of the length we're painting to get ready to paint the row. 63 double centerOffset = ( (dotsToPaint * dot.radius()) / 2) - dot.radius()/2; 64 painter.mbk(centerOffset); 65 66 // Paint the row of dots. 67 int painted = 0; 68 while (painted < dotsToPaint){ 69 paintOneDot(painter, dot); 70 painter.mfd(dot.radius()); 71 painted = painted + 1; 72 } 73 74 // Make the method invariant with respect to painter position. 75 painter.mbk(centerOffset + dot.radius()); 76 } 77 78 private void paintOneDot(SPainter painter, SCircle dot){ 79 dot.s3(); 80 painter.paint(dot); 81 82 dot.x3(); 83 } 84 85 private static int dotsOnLineCount(double lineLength, double sideLength){ 86 int dots = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 87 return dots; 88 } 89 90 private double chordLength(double yOffset, SCircle circle){ 91 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 92 double chordLength = xLength * 2; 93 return chordLength; 94 } 95 96 private static int getNumber(String prompt) { 97 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 98 Scanner scanner = new Scanner(nss); 99 return scanner.nextInt(); 100 } 101 102 private static Color getColor(String prompt) { 103 String nss = JOptionPane.showInputDialog(null, prompt+"?"); 104 Scanner scanner = new Scanner(nss); 105 String response = scanner.next(); 106 Color color = Color.black; 107 if (response.equalsIgnoreCase("red")) 108 { 109 color = Color.RED; 110 } 111 else if (response.equalsIgnoreCase(("blue"))) 112 { 113 color = Color.BLUE; 114 } 115 else if(response.equalsIgnoreCase("green")) 116 { 117 color = Color.GREEN; 118 } 119 return color; 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 }