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