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