1 package npw; 2 /* 3 * A program to paint, centered on the canvas, a circle of colored circles 4 * In this program, we will add spaces between each circle 5 */ 6 7 import painter.SPainter; 8 import shapes.SCircle; 9 import shapes.SSquare; 10 import javax.swing.*; 11 import java.awt.*; 12 import java.util.Random; 13 import java.util.Scanner; 14 15 public class SimpleDots { 16 private void paintTheImage(){ 17 // Get the input info 18 int bigRadius = getNumber("Overall circle radius"); 19 int individualRadius = getNumber("Individual circle radius"); 20 String color = getColor("What color circles"); 21 22 // Establish a painter 23 SPainter painter = new SPainter("Circle of circles", bigRadius*2+50, bigRadius*2+50); 24 painter.setBrushWidth(3); 25 SCircle totalCircle = new SCircle(bigRadius); 26 SCircle individualCircle = new SCircle(individualRadius); 27 28 // Paint the squares 29 paintSimpleDots(painter, totalCircle, individualCircle, color); 30 } 31 32 private void paintSimpleDots(SPainter painter, SCircle bCircle, SCircle iCircle, String color){ 33 // Position the painter to begin drawing rows 34 painter.mfd(bCircle.radius()); 35 painter.tr(); 36 // Paint the circle of squares 37 double moved = 0; 38 while (moved < bCircle.diameter()) { 39 double chord = chordLength(bCircle.radius() - moved, bCircle); 40 int squares = squaresOnLineCount(chord, iCircle.diameter()); 41 paintRow(painter, iCircle, squares, color); 42 nextRow(painter, iCircle.diameter()); 43 moved = moved + iCircle.diameter(); 44 } 45 // Invariance 46 painter.tl(); 47 painter.mfd(bCircle.radius()); 48 } 49 50 // Move to next row 51 private void nextRow(SPainter painter, double rowHeight){ 52 painter.tr(); 53 painter.mfd(rowHeight); 54 painter.tl(); 55 } 56 57 // Assumes the painter is at the center of the row to paint, facing right. 58 private void paintRow(SPainter painter, SCircle iCircle, int squaresToPaint, String color){ 59 // Move backward 1/2 of the length we're painting to get ready to paint the row. 60 double centerOffset = ( (squaresToPaint * iCircle.diameter()) / 2) - iCircle.diameter()/2; 61 painter.mbk(centerOffset); 62 63 // Paint the row of circles. 64 int painted = 0; 65 while (painted < squaresToPaint){ 66 paintOneCircle(painter, iCircle, color); 67 painter.mfd(iCircle.diameter()); 68 painted = painted + 1; 69 } 70 71 // Make the method invariant w/ respect to painter position. 72 painter.mbk(centerOffset + iCircle.diameter()); 73 } 74 75 private void paintOneCircle(SPainter painter, SCircle iCircle, String color){ 76 iCircle.s2(); 77 if(color == null){color = "exit";} //user clicked on cancel 78 if(color.equalsIgnoreCase("red")){ 79 painter.setColor(Color.RED); 80 painter.paint(iCircle); 81 } 82 else if(color.equalsIgnoreCase("blue")){ 83 painter.setColor(Color.BLUE); 84 painter.paint(iCircle); 85 } 86 else if(color.equalsIgnoreCase("green")){ 87 painter.setColor(Color.GREEN); 88 painter.paint(iCircle); 89 } 90 else if(color.equalsIgnoreCase("help")){ 91 JOptionPane.showMessageDialog(null, "Valid colors are: " + "RED | GREEN | BLUE | HELP | EXIT "); 92 } 93 else if(color.equalsIgnoreCase("exit")){ 94 painter.end(); 95 System.out.println("Thank you for viewing the circles..."); 96 } 97 else { 98 painter.setColor(Color.BLACK); 99 painter.paint(iCircle); 100 //Could implement error message if desired: 101 //JOptionPane.showMessageDialog(null, "Unrecognizable command: "); 102 } 103 104 //Remove random colors 105 //painter.setColor(randomColor()); 106 //painter.paint(iCircle); 107 108 //Remove black outline from circles: 109 //painter.setColor(Color.BLACK); 110 //painter.draw(iCircle); 111 112 iCircle.x2(); 113 } 114 115 private static int squaresOnLineCount(double lineLength, double sideLength){ 116 int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 117 return circles; 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 String getColor(String prompt) { 133 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 134 Scanner scanner = new Scanner(nss); 135 return scanner.next(); 136 } 137 138 private static Color randomColor() { 139 Random rgen = new Random(); 140 int r = rgen.nextInt(256); 141 int g = rgen.nextInt(256); 142 int b = rgen.nextInt(256); 143 return new Color(r,g,b); 144 } 145 146 public SimpleDots() { 147 paintTheImage(); 148 } 149 150 public static void main(String[] args) { 151 SwingUtilities.invokeLater(new Runnable() { 152 public void run() { 153 new SimpleDots(); 154 } 155 }); 156 } 157 } 158