SimpleDots.java
1    /* 
2    * Program to paint, centered on the canvas, a circle with circles of a color, with spaces inbetween them sort of 
3     */
4    package npw;
5    
6    import painter.SPainter;
7    import shapes.SCircle;
8    
9    import javax.swing.*;
10   import java.awt.*;
11   import java.util.Random;
12   import java.util.Scanner;
13   
14   public class SimpleDots {
15       private void paintTheImage(){
16           int radius = getNumber("circle radius");
17           int rad = getNumber("dot diameter length");
18   
19           SPainter painter = new SPainter("Simple Dots", radius*2+50, radius*2+50);
20           painter.setBrushWidth(3);
21           SCircle circle = new SCircle(radius);
22           SCircle dot = new SCircle(rad);
23   
24           int colors = getNumber("1 for red, 2 for blue, 3 for green");
25           if (colors == 1) {
26               painter.setColor(Color.RED);
27           } else if (colors == 2) {
28               painter.setColor(Color.BLUE);
29           } else if (colors == 3) {
30               painter.setColor(Color.GREEN);
31           } else {
32               painter.setColor(Color.BLACK);
33           }
34   
35           paintCircleOfDots(painter,circle,dot);
36       }
37   
38   
39   
40       private void paintCircleOfDots(SPainter painter, SCircle circle, SCircle dot) {
41           painter.mfd(circle.radius());
42           painter.tr();
43   
44           double moved = 0;
45                   while ( moved < circle.diameter()) {
46                       double chord = chordLength(circle.radius() - moved, circle);
47                       int dotz = dotsOnLineCount(chord, dot.radius());
48                       paintRow(painter,dot,dotz);
49                       nextRow(painter, dot.radius());
50                       moved = moved + dot.radius();
51                   }
52                   painter.tl();
53                   painter.mfd(circle.radius());
54       }
55   
56   
57   
58   
59       private void nextRow(SPainter painter, double rowHeight) {
60           painter.tr();
61           painter.mfd(rowHeight);
62           painter.tl();
63       }
64       private void paintRow(SPainter painter, SCircle dot, int dotsToPaint) {
65           double centerOffset = ( ( dotsToPaint * dot.radius()) / 2) - dot.radius()/2;
66           painter.mbk(centerOffset);
67   
68           int painted = 0;
69           while (painted < dotsToPaint){
70               paintOneSquare(painter, dot);
71               painter.mfd(dot.radius());
72               painted = painted +1;
73           }
74   
75           painter.mbk(centerOffset + dot.radius());
76       }
77   
78       private void paintOneSquare(SPainter painter, SCircle dot) {
79           dot.s3();
80           painter.paint(dot);
81           dot.x3();
82   
83   
84       }
85   
86   
87       private int dotsOnLineCount(double lineLength, double sideLength) {
88           int circles = ( (int) Math.ceil( (lineLength-sideLength )/ sideLength) + 1);
89           return circles;
90       }
91       private double chordLength(double yOffSet, SCircle circle) {
92           double xLength = Math.sqrt(Math.pow(circle.radius(), 2) - Math.pow(yOffSet, 2));
93           double chordLength = xLength * 2;
94           return chordLength;
95       }
96       private int getNumber(String prompt) {
97           String nss = JOptionPane.showInputDialog(null,prompt+"?");
98           Scanner scanner = new Scanner(nss);
99           return scanner.nextInt();
100  
101      }
102  
103     /* private Color randomColor() { 
104          Random rgen = new Random(); 
105          int r = rgen.nextInt(256); 
106          int g = rgen.nextInt(256); 
107          int b = rgen.nextInt(256); 
108          return new Color(r,g,b);} 
109      */
110  
111      public SimpleDots() {
112          paintTheImage();
113      }
114      
115      public static void main(String[] args) {
116          SwingUtilities.invokeLater(new Runnable() {
117              public void run() {
118                  new SimpleDots();
119                  
120              }
121          });
122      }
123  }
124