1 package npw; 2 3 import painter.SPainter; 4 import shapes.SCircle; 5 import shapes.SSquare; 6 7 import javax.swing.*; 8 import java.awt.*; 9 import java.util.Random; 10 import java.util.Scanner; 11 12 13 public class SimpleDots { 14 private void paintTheImage(){ 15 // Get input information 16 int radius = getNumber("Radius of Big Circle"); 17 int smallradius = getNumber("Radius of smaller circles"); 18 String dotcolor = getDotcolor( "Color of Dots (RED, GREEN, or BLUE)"); 19 // establish the painter. 20 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 21 painter.setBrushWidth(3); 22 if ( dotcolor.equalsIgnoreCase("RED")) { 23 painter.setColor(Color.RED); 24 } else if ( dotcolor.equalsIgnoreCase("GREEN")) { 25 painter.setColor(Color.GREEN); 26 } else if (dotcolor.equalsIgnoreCase("BLUE")) { 27 painter.setColor(Color.BLUE); 28 } else { 29 painter.setColor(Color.BLACK); 30 } 31 32 SCircle circle = new SCircle(radius); 33 SCircle square = new SCircle(smallradius); 34 35 paintCircleOfSquares(painter,circle,square); 36 37 } 38 39 private String getDotcolor(String prompt) { 40 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 41 Scanner scanner = new Scanner(nss); 42 return scanner.next(); 43 } 44 45 private int getNumber(String prompt) { 46 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 47 Scanner scanner = new Scanner(nss); 48 return scanner.nextInt(); 49 50 } 51 52 53 private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle square) { 54 painter.mfd(circle.radius()); 55 painter.tr(); 56 // paint the stuff 57 double moved = 0; 58 while (moved < circle.diameter()) { 59 double chord = chordLength(circle.radius() - moved, circle); 60 int squares = squaresOnLineCount(chord, square.diameter()); 61 paintRow(painter, square, squares); 62 nextRow(painter,square.diameter()); 63 moved = moved + square.diameter(); 64 65 } 66 painter.tl(); 67 painter.mfd(circle.radius()); 68 69 } 70 71 private double chordLength(double yOffset, SCircle circle) { 72 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 73 double chordLength = xLength * 2; 74 return chordLength; 75 } 76 77 private int squaresOnLineCount(double lineLength, double sideLength) { 78 int squares = ( (int)Math.ceil( ( lineLength - sideLength) / sideLength ) + 1); 79 return squares; 80 } 81 82 private void paintRow(SPainter painter, SCircle square, int squaresToPaint) { 83 double centerOffSet = ( (squaresToPaint * square.diameter()) / 2) - square.diameter()/2; 84 painter.mbk(centerOffSet); 85 int painted = 0; 86 while (painted < squaresToPaint){ 87 paintOneSquare(painter, square); 88 painter.mfd(square.diameter()); 89 painted = painted + 1; 90 91 } 92 painter.mbk(centerOffSet + square.diameter()); 93 94 } 95 96 private void paintOneSquare(SPainter painter, SCircle square) { 97 SCircle Test = new SCircle((square.radius()) /2 ); 98 painter.paint(Test); 99 } 100 101 private void nextRow(SPainter painter, double rowHeight) { 102 painter.tr(); 103 painter.mfd(rowHeight); 104 painter.tl(); 105 106 } 107 public SimpleDots() { 108 paintTheImage(); 109 } 110 public static void main(String[] args) { 111 SwingUtilities.invokeLater(new Runnable() { 112 public void run() { 113 new SimpleDots(); 114 } 115 }); 116 } 117 }