1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 import javax.swing.*; 6 import java.awt.*; 7 import java.util.Random; 8 import java.util.Scanner; 9 10 public class SimpleDots { 11 12 private void paintTheImage(){ 13 // Get the input information 14 int radius = getNumber("circle radius"); 15 int dotradius = getNumber("dots diameter"); 16 String color = getColor("Pick a color out of Red, Blue, or Green "); 17 18 // Establish the painter 19 SPainter painter = new SPainter("Circle of dots", radius*2+50, radius*2+50); 20 painter.setBrushWidth(3); 21 SCircle circle = new SCircle(radius); 22 SCircle dot = new SCircle(dotradius); 23 24 // Paint the squares 25 paintHirstSquares(painter, circle, dot, color); 26 } 27 28 private String getColor(String s) { 29 String color = JOptionPane.showInputDialog(null,s+"?"); 30 Scanner scanner = new Scanner(color); 31 return scanner.next(); 32 } 33 34 private void paintHirstSquares(SPainter painter, SCircle circle, SCircle dot, String color){ 35 // Paint the circle of squares 36 double howFarToMove = 0; 37 38 while (howFarToMove < circle.radius()) { 39 double chord = chordLength(howFarToMove, circle); 40 int dots = circleOnLineCount(chord, dot.diameter()); 41 if (howFarToMove == 0) { 42 paintRow(painter, dot, dots, color); 43 } 44 else { 45 // Stay invariant with respect to painter position 46 painter.mfd(howFarToMove); 47 paintRow(painter, dot, dots, color ); 48 painter.mbk(howFarToMove * 2); 49 paintRow(painter, dot, dots, color); 50 painter.mfd(howFarToMove); 51 } 52 howFarToMove = howFarToMove + dot.diameter(); 53 } 54 } 55 56 // Assumes the painter is at the center of the row to paint, facing right. 57 private void paintRow(SPainter painter, SCircle dot, int squaresToPaint, String color){ 58 // Turn right to start painting the row. 59 painter.tr(); 60 // Move backward 1/2 of the length we're painting to get ready to paint the row. 61 double centerOffset = ( (squaresToPaint * dot.diameter()) / 2) - dot.diameter()/2; 62 painter.mbk(centerOffset); 63 64 // Paint the row of squares. 65 int painted = 0; 66 while (painted < squaresToPaint){ 67 paintOneSquare(painter, dot, color); 68 painter.mfd(dot.diameter()); 69 painted = painted + 1; 70 } 71 72 // Make the method invariant with respect to painter position. 73 painter.mbk(centerOffset + dot.diameter()); 74 // Turn left to stay invariant with respect to direction. 75 painter.tl(); 76 } 77 78 private void paintOneSquare(SPainter painter,SCircle dot, String color){ 79 if (color.equalsIgnoreCase("Blue")) { 80 painter.setColor(Color.BLUE); 81 } else if (color.equalsIgnoreCase("Red")) { 82 painter.setColor(Color.RED); 83 } else if (color.equalsIgnoreCase("Green")) { 84 painter.setColor(Color.GREEN); 85 } else { 86 painter.setColor(Color.BLACK); 87 } 88 dot.s2(); 89 painter.paint(dot); 90 dot.x2(); 91 } 92 93 private static int circleOnLineCount(double lineLength, double sideLength){ 94 int dots = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 95 return dots; 96 } 97 98 private double chordLength(double yOffset, SCircle circle){ 99 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 100 double chordLength = xLength * 2; 101 return chordLength; 102 } 103 104 private static int getNumber(String prompt) { 105 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 106 Scanner scanner = new Scanner(nss); 107 return scanner.nextInt(); 108 } 109 110 private static Color randomColor() { 111 Random rgen = new Random(); 112 int r = rgen.nextInt(256); 113 int g = rgen.nextInt(256); 114 int b = rgen.nextInt(256); 115 return new Color(r,g,b); 116 } 117 118 public SimpleDots() { 119 paintTheImage(); 120 } 121 122 public static void main(String[] args) { 123 SwingUtilities.invokeLater(new Runnable() { 124 public void run() { 125 new SimpleDots(); 126 } 127 }); 128 } 129 } 130