1 /* 2 * A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares. 3 */ 4 5 package npw; 6 7 import painter.SPainter; 8 import shapes.SCircle; 9 10 import javax.swing.*; 11 import java.awt.*; 12 import java.util.Random; 13 import java.util.Scanner; 14 15 public class SimpleDots { 16 17 private void paintTheImage(){ 18 // Get the input information 19 int bigRadius = getNumber("circle radius"); 20 int smallRadius = getNumber("circle radius"); 21 22 String colorStr = JOptionPane.showInputDialog(null, "Color of circles"); 23 24 Color color = new Color(0, 0, 0); 25 26 if (colorStr.equalsIgnoreCase("blue")) { color = Color.BLUE; } 27 else if (colorStr.equalsIgnoreCase("red")) { color = Color.RED; } 28 else if (colorStr.equalsIgnoreCase("green")) { color = Color.GREEN; } 29 else { color = Color.BLACK; } 30 31 System.out.println(colorStr); 32 33 // Establish the painter 34 SPainter painter = new SPainter("Circle of Squares", bigRadius*2+50, bigRadius*2+50); 35 painter.setBrushWidth(1); 36 SCircle bigCircle = new SCircle(bigRadius); 37 SCircle smallCirlce = new SCircle(smallRadius); 38 39 // Paint the squares 40 paintCircleOfCirlces(painter, bigCircle, smallCirlce, color); 41 } 42 43 private void paintCircleOfCirlces(SPainter painter, SCircle bigCircle, SCircle smallCircle, Color color){ 44 // Position the painter to begin drawing the rows 45 painter.mfd(bigCircle.radius()); 46 painter.tr(); 47 // Paint the circle of squares 48 double moved = 0; 49 while (moved < bigCircle.diameter()) { 50 double chord = chordLength(bigCircle.radius() - moved, bigCircle); 51 int squares = squaresOnLineCount(chord, smallCircle.diameter()); 52 paintRow(painter, smallCircle, squares, color); 53 nextRow(painter, smallCircle.diameter()); 54 moved = moved + smallCircle.diameter(); 55 } 56 // Make the method invariant with respect to painter position 57 painter.tl(); 58 painter.mfd(bigCircle.radius()); 59 } 60 61 // Move to the next row 62 private void nextRow(SPainter painter, double rowHeight){ 63 painter.tr(); 64 painter.mfd(rowHeight); 65 painter.tl(); 66 } 67 68 // Assumes the painter is at the center of the row to paint, facing right. 69 private void paintRow(SPainter painter, SCircle circle, int squaresToPaint, Color color){ 70 // Move backward 1/2 of the length we're painting to get ready to paint the row. 71 double centerOffset = ( (squaresToPaint * circle.diameter()) / 2) - circle.diameter()/2; 72 painter.mbk(centerOffset); 73 74 // Paint the row of squares. 75 int painted = 0; 76 while (painted < squaresToPaint){ 77 paintOneCircle(painter, circle, color); 78 painter.mfd(circle.diameter()); 79 painted = painted + 1; 80 } 81 82 // Make the method invariant with respect to painter position. 83 painter.mbk(centerOffset + circle.diameter()); 84 } 85 86 private void paintOneCircle(SPainter painter, SCircle square, Color color){ 87 painter.setColor(color); 88 square.s2(); 89 painter.paint(square); 90 painter.setColor(Color.BLACK); 91 square.x2(); 92 } 93 94 private static int squaresOnLineCount(double lineLength, double sideLength){ 95 int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 96 return squares; 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 Color randomColor() { 112 Random rgen = new Random(); 113 int r = rgen.nextInt(256); 114 int g = rgen.nextInt(256); 115 int b = rgen.nextInt(256); 116 return new Color(r,g,b); 117 } 118 119 public SimpleDots() { 120 paintTheImage(); 121 } 122 123 public static void main(String[] args) { 124 SwingUtilities.invokeLater(new Runnable() { 125 public void run() { 126 new SimpleDots(); 127 } 128 }); 129 } 130 } 131