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 private void paintTheImage() { 17 // get input 18 int radius = getNumber("circle radius"); 19 int side = getNumber("circle radius"); 20 21 // Establish the painter 22 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 23 painter.setBrushWidth(3); 24 SCircle bigCircle = new SCircle(radius); 25 SCircle littleCircle = new SCircle(side); 26 27 // paint the squares 28 paintCircleOfSquares(painter, bigCircle, littleCircle); 29 } 30 31 // Ask user for input 32 public static String userInput(String prompt) { 33 String nss = JOptionPane.showInputDialog(null, prompt); 34 Scanner scanner = new Scanner(nss); 35 return scanner.next(); 36 } 37 38 39 private void paintCircleOfSquares(SPainter painter, SCircle bigCircle, SCircle littleCircle) { 40 // position the painter to begin drawing the rows 41 painter.mfd(bigCircle.radius()); 42 painter.tr(); 43 String userText = userInput("Enter a color?"); 44 if (userText.equalsIgnoreCase("red")) { 45 painter.setColor(Color.red); 46 } 47 else if (userText.equalsIgnoreCase("blue")) { 48 painter.setColor(Color.blue); 49 } 50 else if (userText.equalsIgnoreCase("green")) { 51 painter.setColor(Color.green); 52 }else { 53 System.out.println("Invalid color! Please enter a color of blue, red, or green."); 54 } 55 // paint the circle of squares 56 double moved = 0; 57 while ( moved < bigCircle.diameter() ) { 58 double chord = chordLength(bigCircle.radius() - moved, bigCircle); 59 int squares = squaresOnLineCount(chord, littleCircle.radius()*2); 60 paintRow(painter, littleCircle, squares); 61 nextRow(painter, littleCircle.radius()*2); 62 moved = moved + littleCircle.radius()*2; 63 } 64 // make the method with respect to painter position 65 painter.tl(); 66 painter.mfd(bigCircle.radius()); 67 } 68 69 // Move to the next row 70 private void nextRow(SPainter painter, double rowHeight) { 71 painter.tr(); 72 painter.mfd(rowHeight); 73 painter.tl(); 74 } 75 76 // Assumes the painter is at the center of the row to paint, facing right. 77 private void paintRow(SPainter painter, SCircle circle, int squaresToPaint) { 78 79 // Move backward 1/2 of the length in respect to painting to get ready to paint row. 80 double centerOffSet = ( (squaresToPaint * circle.radius()) - circle.radius()); 81 painter.mbk(centerOffSet); 82 83 // Paint the row of squares. 84 int painted = 0; 85 while ( painted < squaresToPaint ) { 86 paintOneSquare(painter, circle); 87 painter.mfd(circle.radius()*2); 88 painted = painted + 1; 89 } 90 // Make the method in respect to painted position. 91 painter.mbk(centerOffSet + circle.radius()*2); 92 } 93 94 private void paintOneSquare(SPainter painter, SCircle circle) { 95 96 circle.s2(); 97 painter.paint(circle); 98 99 circle.x2(); 100 } 101 102 private static int squaresOnLineCount(double lineLength, double sideLength) { 103 int circle = ( (int)Math.ceil(( lineLength - sideLength) / sideLength)+1); 104 return circle; 105 } 106 107 private double chordLength(double yOffset, SCircle circle) { 108 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 109 double chordLength = xLength * 2; 110 return chordLength; 111 } 112 113 public static int getNumber(String prompt) { 114 String nss = JOptionPane.showInputDialog(null, prompt+"?"); 115 Scanner scanner = new Scanner(nss); 116 return scanner.nextInt(); 117 } 118 119 public static Color randomColor() { 120 Random rgen = new Random(); 121 int r = rgen.nextInt(256); 122 int g = rgen.nextInt(256); 123 int b = rgen.nextInt(256); 124 125 return new Color(r,g,b); 126 } 127 128 public simpleDots() { 129 paintTheImage(); 130 } 131 132 public static void main(String[] args) { 133 SwingUtilities.invokeLater(new Runnable() { 134 public void run() { 135 new simpleDots(); 136 } 137 }); 138 } 139 } 140