SimpleDots.java
1    
2    
3    /* 
4     * A program to paint, centered on the canvas, a circle of colored dots of which 
5     * the color, number of dots, and radius of circle are determined by user input. 
6     */
7    
8    package npw;
9    
10   import painter.SPainter;
11   import shapes.SCircle;
12   import javax.swing.*;
13   import java.awt.*;
14   
15   import java.util.Scanner;
16   
17   public class SimpleDots {
18   
19       private void paintTheImage() {
20           // Get the input information
21   
22           int radius = getNumber("circle radius");
23           int dotr = getNumber("dot radius");
24           //String userC = getColor("Green, Red, or Blue");
25   
26           // Establish the painter
27           SPainter painter = new SPainter("Circle of Dots", radius * 2 + 50, radius * 2 + 50);
28           painter.setBrushWidth(3);
29           SCircle circle = new SCircle(radius);
30           SCircle dot = new SCircle(dotr);
31   
32           String command = JOptionPane.showInputDialog(null, "Red, Green, Blue?");
33           if (command.equalsIgnoreCase("blue")) {
34               painter.setColor(Color.BLUE);
35           } else if (command.equalsIgnoreCase("red")) {
36               painter.setColor(Color.RED);
37           } else if (command.equalsIgnoreCase("green")) {
38               painter.setColor(Color.GREEN);
39           } else {
40               painter.setColor(Color.BLACK);
41           }
42   
43   
44           paintCircleOfSquares(painter, circle, dot);
45   
46   
47           }
48   
49   
50       private void paintCircleOfSquares(SPainter painter, SCircle circle, SCircle dot) {
51           // Position the painter to begin drawing the rows
52           painter.mfd(circle.radius());
53           painter.tr();
54           // Paint the circle of squares
55           double moved = 0;
56           while (moved < circle.diameter()) {
57               double chord = chordLength(circle.radius() - moved, circle);
58               int dots = squaresOnLineCount(chord, dot.diameter());
59               paintRow(painter, dot, dots);
60               nextRow(painter, dot.diameter());
61               moved = moved + dot.diameter();
62           }
63           // Make the method invariant with respect to painter position
64           painter.tl();
65           painter.mfd(circle.radius());
66       }
67   
68       // Move to the next row
69       private void nextRow(SPainter painter, double rowHeight) {
70           painter.tr();
71           painter.mfd(rowHeight);
72           painter.tl();
73       }
74   
75       // Assumes the painter is at the center of the row to paint, facing right.
76       private void paintRow(SPainter painter, SCircle dot, int squaresToPaint) {
77           // Move backward 1/2 of the length we're painting to get ready to paint the row.
78           double centerOffset = ((squaresToPaint * dot.diameter()) / 2) - dot.diameter() / 2;
79           painter.mbk(centerOffset);
80   
81           // Paint the row of squares.
82           int painted = 0;
83           while (painted < squaresToPaint) {
84               paintOneSquare(painter, dot);
85               painter.mfd(dot.diameter());
86               painted = painted + 1;
87           }
88   
89           // Make the method invariant with respect to painter position.
90           painter.mbk(centerOffset + dot.diameter());
91       }
92   
93       private void paintOneSquare(SPainter painter, SCircle dot) {
94   
95   
96           dot.s2();
97           painter.paint(dot);
98           dot.x2();
99       }
100  
101      private static int squaresOnLineCount(double lineLength, double sideLength) {
102          int dots = ((int) Math.ceil((lineLength - sideLength) / sideLength) + 1);
103          return dots;
104      }
105  
106      private double chordLength(double yOffset, SCircle circle) {
107          double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffset, 2));
108          double chordLength = xLength * 2;
109          return chordLength;
110      }
111  
112      private static int getNumber(String prompt) {
113          String nss = JOptionPane.showInputDialog(null, prompt + "?");
114          Scanner scanner = new Scanner(nss);
115          return scanner.nextInt();
116      }
117  
118  
119  
120      public SimpleDots() {
121          paintTheImage();
122      }
123  
124      public static void main(String[] args) {
125          SwingUtilities.invokeLater(new Runnable() {
126              public void run() {
127                  new SimpleDots();
128              }
129          });
130      }
131  }