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