1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.util.Scanner; 9 10 public class SimpleDots { 11 12 private void paintTheImage(){ 13 int radius1 = getNumber("circle radius"); 14 int radius2 = getNumber("circle radius length"); 15 Color color = getColor("circle color"); 16 SPainter painter = new SPainter("Simple Dots", radius1*2+50, radius1*2+50); 17 painter.setBrushWidth(3); 18 SCircle circle1 = new SCircle(radius1); 19 SCircle circle2 = new SCircle(radius2); 20 paintCircleOfCircles(painter, circle1, circle2, color); 21 } 22 23 private void paintCircleOfCircles(SPainter painter, SCircle circle1, SCircle circle2, Color color){ 24 painter.mfd(circle1.radius()); 25 painter.tr(); 26 double moved = 0; 27 while (moved < circle1.diameter()) { 28 double chord = chordLength(circle1.radius() - moved, circle1); 29 int circles = circlesOnLineCount(chord, circle2.diameter()); 30 paintRow(painter, circle2, circles, color); 31 nextRow(painter, circle2.diameter()); 32 moved = moved + circle2.diameter(); 33 } 34 painter.tl(); 35 painter.mfd(circle1.radius()); 36 } 37 38 private void nextRow(SPainter painter, double rowHeight){ 39 painter.tr(); 40 painter.mfd(rowHeight); 41 painter.tl(); 42 } 43 44 private void paintRow(SPainter painter, SCircle circle2, int circlesToPaint, Color color){ 45 double centerOffset = ( (circlesToPaint * circle2.diameter()) / 2) - circle2.diameter()/2; 46 painter.mbk(centerOffset); 47 int painted = 0; 48 while (painted < circlesToPaint){ 49 paintOneCircle(painter, circle2, color); 50 painter.mfd(circle2.diameter()); 51 painted = painted + 1; 52 } 53 painter.mbk(centerOffset + circle2.diameter()); 54 } 55 56 private void paintOneCircle(SPainter painter, SCircle circle2, Color color){ 57 circle2.s2(); 58 painter.setColor(color); 59 painter.paint(circle2); 60 circle2.x2(); 61 } 62 63 private static int circlesOnLineCount(double lineLength, double sideLength){ 64 int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 65 return circles; 66 } 67 68 private double chordLength(double yOffset, SCircle circle1){ 69 double xLength = Math.sqrt(Math.pow(circle1.radius(), 2) - Math.pow(yOffset, 2)); 70 double chordLength = xLength * 2; 71 return chordLength; 72 } 73 74 private static int getNumber(String prompt) { 75 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 76 Scanner scanner = new Scanner(nss); 77 return scanner.nextInt(); 78 } 79 80 private static Color getColor(String prompt) { 81 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 82 Scanner scanner = new Scanner(nss); 83 String color = scanner.next(); 84 while(true) { 85 if(color.equalsIgnoreCase("BLUE")) { 86 return new Color(0,0,255); 87 } else if(color.equalsIgnoreCase("RED")) { 88 return new Color(255,0,0); 89 } else if(color.equalsIgnoreCase("GREEN")) { 90 return new Color(0,255,0); 91 } else { 92 return new Color(0,0,0); 93 } 94 } 95 } 96 97 public SimpleDots() { 98 paintTheImage(); 99 } 100 101 public static void main(String[] args) { 102 SwingUtilities.invokeLater(new Runnable() { 103 public void run() { 104 new SimpleDots(); 105 } 106 }); 107 } 108 }