1 /* 2 * A program to paint, centered on the canvas, a circle of uniformly colored, unframed dots; a simple variant of the HirstDots program. 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 import javax.swing.JOptionPane; 14 15 public class SimpleDots { 16 17 18 private void paintTheImage() { 19 // Get the input information 20 int radius = getNumber("circle radius"); 21 int radius2 = getNumber("circle radius2"); 22 String color = getColor("Red | Blue | Green "); 23 24 // Establish the painter 25 SPainter painter = new SPainter("Simple Dots", radius * 2 + 50, radius * 2 + 50); 26 painter.setBrushWidth(3); 27 SCircle circle = new SCircle(radius); 28 SCircle dot = new SCircle(radius2); 29 30 // Paint the squares 31 paintSimpleDots(painter, circle, dot, color); 32 33 } 34 35 private void paintSimpleDots(SPainter painter, SCircle circle, SCircle dot, String color) { 36 // Position the painter to begin drawing the rows 37 painter.mfd(circle.radius()); 38 painter.tr(); 39 // Paint the circle of dots- Hirst 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, color); 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, String color) { 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, color); 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, 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.s3(); 89 painter.paint(dot); 90 //painter.draw(dot); 91 dot.x3(); 92 } 93 94 private static int dotsOnLineCount(double lineLength, double sideLength) { 95 int dots = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1); 96 return dots; 97 } 98 99 private double chordLength(double yOffset, SCircle circle) { 100 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 101 double chordLength = xLength * 2; 102 return chordLength; 103 } 104 105 private static int getNumber(String prompt) { 106 String nss = JOptionPane.showInputDialog(null, prompt + "?"); 107 Scanner scanner = new Scanner(nss); 108 return scanner.nextInt(); 109 } 110 111 private static String getColor (String prompt){ 112 String color = JOptionPane.showInputDialog(null,prompt+"?"); 113 Scanner scanner = new Scanner(color); 114 return scanner.next(); 115 } 116 private static Color randomColor() { 117 Random rgen = new Random(); 118 int r = rgen.nextInt(256); 119 int g = rgen.nextInt(256); 120 int b = rgen.nextInt(256); 121 return new Color(r, g, b); 122 } 123 124 public SimpleDots() { 125 paintTheImage(); 126 } 127 128 public static void main(String[] args) { 129 SwingUtilities.invokeLater(new Runnable() { 130 public void run() { 131 new SimpleDots(); 132 } 133 }); 134 } 135 }