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 import shapes.SSquare; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Random; 14 import java.util.Scanner; 15 16 public class SimpleDots { 17 18 private void paintTheImage(){ 19 // Get the input information 20 int radius = getNumber("circle radius"); 21 int side = (getNumber("small circle radius")) * 2; 22 int ColorNumber = getNumber("(1 = Red | 2 = Blue | 3 = Green | 4 = Black | 5 = Cyan | 6 = Gray | 7 = Orange | 8 = Pink | 9 = Yellow) Color"); 23 24 // Establish the painter 25 SPainter painter = new SPainter("Simple Dots", radius*2+50, radius*2+50); 26 painter.setBrushWidth(3); 27 SCircle circle = new SCircle(radius); 28 SSquare square = new SSquare(side); 29 30 // Set the painter color 31 SetPainterColor(ColorNumber, painter); 32 33 // Paint the squares 34 paintCircleOfSquares(painter, circle, square); 35 } 36 37 private void paintCircleOfSquares(SPainter painter, SCircle circle, SSquare square){ 38 // Position the painter to begin drawing the rows 39 painter.mfd(circle.radius()); 40 painter.tr(); 41 // Paint the circle of squares 42 double moved = 0; 43 while (moved < circle.diameter()) { 44 double chord = chordLength(circle.radius() - moved, circle); 45 int squares = squaresOnLineCount(chord, square.side()); 46 paintRow(painter, square, squares); 47 nextRow(painter, square.side()); 48 moved = moved + square.side(); 49 } 50 // Make the method invariant with respect to painter position 51 painter.tl(); 52 painter.mfd(circle.radius()); 53 } 54 55 // Move to the next row 56 private void nextRow(SPainter painter, double rowHeight){ 57 painter.tr(); 58 painter.mfd(rowHeight); 59 painter.tl(); 60 } 61 62 // Assumes the painter is at the center of the row to paint, facing right. 63 private void paintRow(SPainter painter, SSquare square, int squaresToPaint){ 64 // Move backward 1/2 of the length we're painting to get ready to paint the row. 65 double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2; 66 painter.mbk(centerOffset); 67 68 // Paint the row of squares. 69 int painted = 0; 70 while (painted < squaresToPaint){ 71 paintOneSquare(painter, square); 72 painter.mfd(square.side()); 73 painted = painted + 1; 74 } 75 76 // Make the method invariant with respect to painter position. 77 painter.mbk(centerOffset + square.side()); 78 } 79 80 private void paintOneSquare(SPainter painter, SSquare square){ 81 square.s2(); 82 painter.paint(square.inscribingCircle()); 83 square.x2(); 84 } 85 86 private static int squaresOnLineCount(double lineLength, double sideLength){ 87 int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 88 return squares; 89 } 90 91 private double chordLength(double yOffset, SCircle circle){ 92 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 93 double chordLength = xLength * 2; 94 return chordLength; 95 } 96 97 private static int getNumber(String prompt) { 98 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 99 Scanner scanner = new Scanner(nss); 100 return scanner.nextInt(); 101 } 102 103 private static Color randomColor() { 104 Random rgen = new Random(); 105 int r = rgen.nextInt(256); 106 int g = rgen.nextInt(256); 107 int b = rgen.nextInt(256); 108 return new Color(r,g,b); 109 } 110 111 private static void SetPainterColor(int ColorNumber, SPainter painter){ 112 if (ColorNumber == 1){ 113 painter.setColor(Color.RED); 114 } else if (ColorNumber == 2){ 115 painter.setColor(Color.BLUE); 116 } else if (ColorNumber == 3){ 117 painter.setColor(Color.GREEN); 118 } else if (ColorNumber == 4){ 119 painter.setColor(Color.BLACK); 120 } else if (ColorNumber == 5){ 121 painter.setColor(Color.CYAN); 122 } else if (ColorNumber == 6){ 123 painter.setColor(Color.GRAY); 124 } else if (ColorNumber == 7){ 125 painter.setColor(Color.ORANGE); 126 } else if (ColorNumber == 8){ 127 painter.setColor(Color.PINK); 128 } else if (ColorNumber == 9){ 129 painter.setColor(Color.YELLOW); 130 } else{ 131 painter.setColor(randomColor()); 132 } 133 } 134 135 public SimpleDots() { 136 paintTheImage(); 137 } 138 139 public static void main(String[] args) { 140 SwingUtilities.invokeLater(new Runnable() { 141 public void run() { 142 new SimpleDots(); 143 } 144 }); 145 } 146 }