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