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 radius = getNumber("circle radius"); 20 int diameter = getNumber("Circle Diameter"); 21 double radius2= diameter/2; 22 23 // Establish the painter 24 SPainter painter = new SPainter("Circle of Squares", radius*2+50, radius*2+50); 25 painter.setBrushWidth(3); 26 SCircle circle = new SCircle(radius); 27 SCircle circles= new SCircle(radius2); 28 29 while (true){ 30 String color= JOptionPane.showInputDialog(null,"command?"); 31 System.out.println(color); 32 if(color == null){ 33 color= "exit"; 34 break; 35 }// user clicked on cancel 36 if(color.equalsIgnoreCase("blue")) { 37 painter.setColor(Color.BLUE); 38 39 break; 40 } else if(color.equalsIgnoreCase("Red")) { 41 painter.setColor(Color.RED); 42 43 break; 44 }else if(color.equalsIgnoreCase("Black")) { 45 painter.setColor(Color.BLACK); 46 47 break; 48 }else if(color.equalsIgnoreCase("Green")) { 49 painter.setColor(Color.GREEN); 50 51 break; 52 }else if(color.equalsIgnoreCase("Help")) { 53 JOptionPane.showMessageDialog(null, "valid commands are:" + "red| blue| Black|Green| Help| Exit"); 54 break; 55 }else if(color.equalsIgnoreCase("exit")){ 56 painter.end(); 57 System.out.println(" Thank you for viewing the dots..."); 58 break; 59 60 61 } 62 63 64 65 66 } 67 68 // Paint the squares 69 paintCircleOfSquares(painter, circle, circles); 70 } 71 72 private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle circles){ 73 // Position the painter to begin drawing the rows 74 painter.mfd(circle.radius()); 75 painter.tr(); 76 // Paint the circle of squares 77 double moved = 0; 78 while (moved < circle.diameter()) { 79 double chord = chordLength(circle.radius() - moved, circle); 80 System.out.println(chord); 81 int lineCircle = circleOnLineCount(chord, circles.diameter()); 82 System.out.println(lineCircle); 83 paintRow(painter, circles, lineCircle); 84 nextRow(painter, circles.diameter()); 85 moved = moved + circles.diameter(); 86 } 87 // Make the method invariant with respect to painter position 88 painter.tl(); 89 painter.mfd(circle.radius()); 90 } 91 92 // Move to the next row 93 private void nextRow(SPainter painter, double rowHeight){ 94 painter.tr(); 95 painter.mfd(rowHeight); 96 painter.tl(); 97 } 98 99 // Assumes the painter is at the center of the row to paint, facing right. 100 private void paintRow(SPainter painter, SCircle circles, int circleToPaint){ 101 // Move backward 1/2 of the length we're painting to get ready to paint the row. 102 double centerOffset = ( (circleToPaint * circles.diameter() / 2) - circles.diameter()/2); 103 painter.mbk(centerOffset); 104 105 // Paint the row of squares. 106 int painted = 0; 107 while (painted < circleToPaint){ 108 paintOneCircle(painter,circles); 109 painter.mfd(circles.diameter()); 110 painted = painted + 1; 111 } 112 113 // Make the method invariant with respect to painter position. 114 painter.mbk(centerOffset + circles.diameter()); 115 } 116 117 private void paintOneCircle(SPainter painter, SCircle circles){ 118 //painter.setColor(randomColor()); 119 circles.s3(); 120 painter.paint(circles); 121 circles.x3(); 122 123 } 124 125 private static int circleOnLineCount(double lineLength, double sideLength){ 126 System.out.println(lineLength + " " + sideLength); 127 int circles = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1); 128 return circles; 129 } 130 131 private double chordLength(double yOffset, SCircle circle){ 132 double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2)); 133 double chordLength = xLength * 2; 134 return chordLength; 135 } 136 137 private static int getNumber(String prompt) { 138 String nss = JOptionPane.showInputDialog(null,prompt+"?"); 139 Scanner scanner = new Scanner(nss); 140 return scanner.nextInt(); 141 } 142 143 144 /*private static Color randomColor() { 145 Random rgen = new Random(); 146 int r = rgen.nextInt(256); 147 int g = rgen.nextInt(256); 148 int b = rgen.nextInt(256); 149 return new Color(r,g,b); 150 } 151 */ 152 153 154 public SimpleDots() { 155 paintTheImage(); 156 } 157 158 public static void main(String[] args) { 159 SwingUtilities.invokeLater(new Runnable() { 160 public void run() { 161 new SimpleDots(); 162 } 163 }); 164 } 165 } 166