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