Invention1.java
1    /* 
2     * Program that uses squares and rectangles to make the exact same image every time 
3     */
4    
5    
6    package npw;
7    
8    import painter.SPainter;
9    import shapes.SCircle;
10   import shapes.SSquare;
11   
12   import javax.swing.*;
13   import java.awt.*;
14   
15   public class Invention1 {
16   
17       private void paintTheImage(){
18           // Get the input information
19           int radius = 350;
20           int side = 40;
21           int radius1 = 20;
22   
23           // Establish the painter
24           SPainter painter = new SPainter("Invention1", radius*2+50, radius*2+50);
25           painter.setBrushWidth(3);
26           SCircle circle = new SCircle(radius);
27           SSquare square = new SSquare(side);
28           SCircle dot = new SCircle(radius1);
29   
30           // Paint the squares
31           paintHirstSquares(painter, circle, square, dot, radius1);
32       }
33   
34       private void paintHirstSquares(SPainter painter, SCircle circle, SSquare square, SCircle dot, int radius1){
35           // Position the painter to begin drawing the rows
36           painter.mfd(circle.radius());
37           painter.tr();
38           // Paint the circle of squares
39           double moved = 0;
40           while (moved < circle.diameter()) {
41               double chord = chordLength(circle.radius() - moved, circle);
42               int squares = squaresOnLineCount(chord, square.side());
43               paintRow(painter, square, squares, dot, radius1);
44               nextRow(painter, square.side());
45               moved = moved + square.side();
46           }
47           // Make the method invariant with respect to painter position
48           painter.tl();
49           painter.mfd(circle.radius());
50       }
51   
52       // Move to the next row
53       private void nextRow(SPainter painter, double rowHeight){
54           painter.tr();
55           painter.mfd(rowHeight);
56           painter.tl();
57       }
58   
59       // Assumes the painter is at the center of the row to paint, facing right.
60       private void paintRow(SPainter painter, SSquare square, int squaresToPaint, SCircle dot, int radius1){
61           // Move backward 1/2 of the length we're painting to get ready to paint the row.
62           double centerOffset = ( (squaresToPaint * square.side()) / 2) - square.side()/2;
63           painter.mbk(centerOffset);
64   
65           // Paint the row of squares.
66           int painted = 0;
67           while (painted < squaresToPaint){
68               paintOneSquare(painter, square, dot, radius1);
69               painter.mfd(square.side());
70               painted = painted + 1;
71           }
72   
73           // Make the method invariant with respect to painter position.
74           painter.mbk(centerOffset + square.side());
75       }
76   
77       private void paintOneSquare(SPainter painter, SSquare square, SCircle dot, int radius1){
78           square.s2();
79           if (dot.radius() == radius1) {
80               dot.s3();
81           }else painter.paint(dot);
82           painter.setColor(Color.blue);
83           painter.paint(square);
84           painter.setColor(Color.BLACK);
85           painter.draw(square);
86           painter.setColor(Color.RED);
87           painter.paint(dot);
88           square.x2();
89           dot.x3();
90       }
91       private static int squaresOnLineCount(double lineLength, double sideLength){
92           int squares = ( (int)Math.ceil( ( lineLength - sideLength ) / sideLength ) + 1);
93           return squares;
94       }
95   
96       private double chordLength(double yOffset, SCircle circle){
97           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
98           double chordLength = xLength * 2;
99           return chordLength;
100      }
101  
102      public Invention1() {
103          paintTheImage();
104      }
105  
106      public static void main(String[] args) {
107          SwingUtilities.invokeLater(new Runnable() {
108              public void run() {
109                  new Invention1();
110              }
111          });
112      }
113  }
114