SimpleDots.java
1    /* 
2     *A program to paint, centered on the canvas, a circle of randomly colored, black-framed squares 
3     */
4    package npw;
5    
6    import painter.SPainter;
7    import shapes.SCircle;
8    import shapes.SSquare;
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 the input information
18           int radius = getNumber("circle radius");
19           int side = getNumber("square side length");
20           String command = JOptionPane.showInputDialog(null,"Color?");
21           System.out.println(command);
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           SSquare square = new SSquare(side);
28   
29           //paint the squares
30           paintCircleOfSquare(painter,circle, square,command);
31       }
32       private void paintCircleOfSquare(SPainter painter, SCircle circle, SSquare square,String command){
33           //position the painter to begin drawing the rows
34           painter.mfd(circle.radius());
35           painter.tr();
36           //paint the circle of squares
37           double moved = 0;
38           while(moved < circle.diameter()){
39               double chord = chordLength(circle.radius()-moved, circle);
40               int squares =squaresOnLineCount(chord, square.side());
41               paintRow(painter, square, squares,command);
42               nextRow(painter, square.side());
43               moved = moved + square.side();
44           }
45           //Make the method invariant with respect to painter position
46           painter.tl();
47           painter.mfd(circle.radius());
48       }
49   
50       //Move to the next row
51       private void nextRow(SPainter painter, double rowHeight){
52           painter.tr();
53           painter.mfd(rowHeight);
54           painter.tl();
55       }
56   
57       //Assumes the painter is at the center of the row to paint, facing right.
58       private void paintRow(SPainter painter, SSquare square, int squaresToPaint,String command){
59           //Move backward 1/2 of the length we're painting to get ready to paint the row.
60           double centerOffset = ((squaresToPaint*square.side())/2)-square.side()/2;
61           painter.mbk(centerOffset);
62   
63           //Paint the row of squares
64           int painted = 0;
65           while (painted<squaresToPaint){
66               paintOneSquare(painter, square,command);
67               painter.mfd(square.side());
68               painted = painted + 1;
69           }
70   
71           //Make the method invariant with respect to painter position.
72           painter.mbk(centerOffset+ square.side());
73       }
74       private void paintOneSquare(SPainter painter, SSquare square, String command){
75           square.s2();
76           SCircle dots = square.inscribingCircle();
77           if (command.equalsIgnoreCase("red")){
78               painter.setColor(Color.RED);
79               painter.paint(dots);
80           } else if (command.equalsIgnoreCase("blue")){
81               painter.setColor(Color.BLUE);
82               painter.paint(dots);
83           }else if (command.equalsIgnoreCase("green")){
84               painter.setColor(Color.green);
85               painter.paint(dots);
86           } else {
87               painter.setColor(Color.BLACK);
88               painter.paint(dots);
89           }
90           square.x2();
91       }
92   
93       private static int squaresOnLineCount(double lineLength, double sideLength){
94           int squares = ((int)Math.ceil((lineLength-sideLength)/sideLength)+1);
95           return squares;
96       }
97       private double chordLength(double yOffset,SCircle circle){
98           double xLength = Math.sqrt(Math.pow(circle.radius(),2)-Math.pow(yOffset,2));
99           double chordLength = xLength*2;
100          return chordLength;
101      }
102  
103      private static int getNumber(String prompt){
104          String nss = JOptionPane.showInputDialog(null, prompt+"?");
105          Scanner scanner = new Scanner(nss);
106          return scanner.nextInt();
107  
108      }
109  //    private void paint(SPainter painter, SCircle dots){
110  //        while (true){
111  //            String command = JOptionPane.showInputDialog(null,"Color?");
112  //            if (command==null){
113  //                painter.setColor(Color.BLACK);
114  //                painter.paint(dots);
115  //                break;
116  //            }
117  //           else if (command.equalsIgnoreCase("red")){
118  //                painter.setColor(Color.RED);
119  //                painter.paint(dots);
120  //            }
121  //            else if (command.equalsIgnoreCase("blue")){
122  //                painter.setColor(Color.BLUE);
123  //                painter.paint(dots);
124  //            }else if (command.equalsIgnoreCase("green")){
125  //                painter.setColor(Color.green);
126  //                painter.paint(dots);
127  //            } else {
128  //                break;
129  //            }
130  //        }
131  //    }
132  
133  
134      public SimpleDots(){
135          paintTheImage();
136      }
137      public static void main(String[] args){
138          SwingUtilities.invokeLater(new Runnable() {
139              public void run() {
140                  new SimpleDots();
141              }
142          });
143      }
144  }
145