SimpleDots.java
1    package npw;
2    
3    import painter.SPainter;
4    import  shapes.SCircle;
5    
6    import javax.swing.*;
7    import java.awt.*;
8    import java.util.Random;
9    import java.util.Scanner;
10   
11   import static javax.swing.UIManager.getColor;
12   import static javax.swing.UIManager.getString;
13   
14   
15   public class SimpleDots {
16   
17       public static void main(String[] args) {
18           SwingUtilities.invokeLater(new Runnable() {
19               public void run() {
20                   new SimpleDots();
21               }
22           });
23       }
24   
25       public SimpleDots() {
26           paintTheImage();
27       }
28   
29       private String inputColor = getString( "dot color" );
30       private int dotSpacing = getNumber("column width");
31       private int width = getNumber("width");
32       private int height = getNumber("height");
33   
34       public void paintTheImage() {
35           // Establish the painter
36           SPainter painter = new SPainter("Simple Dots", width, height);
37           SCircle dot = new SCircle(5);
38   
39           // Move the painter to the upper left corner to get ready to paint the gradient
40           painter.mfd(height / 2);
41           painter.tl(90);
42           painter.mfd(width / 2 - 10);
43           painter.tl(90);
44   
45           // Paint it!
46           paintGradient(painter, dot, height, width, dotSpacing);
47       }
48   
49   
50       private static int getNumber(String prompt) {
51           String nss = JOptionPane.showInputDialog(null,prompt+"?");
52           Scanner scanner = new Scanner(nss);
53           return scanner.nextInt();
54       }
55   
56       private void paintGradient(SPainter painter, SCircle dot, int height, int width, int dotSpacing) {
57           int cols = 0;
58           // Calculate the number of columns. We want to fill the screen but we don't want
59           //any of the dots only half on the canvas, so we subtract some space.
60           int nrOfCols = ( width / dotSpacing) - 2;
61   
62           while (cols < nrOfCols){
63               nextCol(painter, dotSpacing);
64               paintColumn(painter, dot, height, dotSpacing);
65               cols = cols + 1;
66           }
67       }
68   
69   
70   
71       // Dots are spaced tighter together near the bottom of the canvas.
72       private void paintColumn(SPainter painter, SCircle dot, int height, int dotSpacing) {
73           int travel = 0;
74           int totalDistanceTraveled = 0;
75   
76           // Paint a row with decreasing distance between dots.
77           while(totalDistanceTraveled < height - (dot.radius() * 3)) {
78               travel = dotSpacing;
79               totalDistanceTraveled = totalDistanceTraveled + travel;
80               painter.mfd(travel);
81               paintOneDot(painter, dot);
82           }
83   
84           // Make the method invariant with respect to painter position.
85           painter.mbk(totalDistanceTraveled);
86   
87       }
88   
89       private void paintOneDot(SPainter painter, SCircle dot) {
90           dynamicColor(painter);
91           painter.paint(dot);
92       }
93   
94       private static String getString(String prompt) {
95           String nss = JOptionPane.showInputDialog (null, prompt+ "?");
96           Scanner scanner = new Scanner(nss);
97           return scanner.next();
98   
99       }
100  
101      private void dynamicColor(SPainter painter) {
102          Random rgen = new Random();
103          if (inputColor.equalsIgnoreCase("red")) {
104              painter.setColor(Color.RED);
105          } else if (inputColor.equalsIgnoreCase("yellow")) {
106              painter.setColor(Color.YELLOW);
107          } else if (inputColor.equalsIgnoreCase("blue")) {
108              painter.setColor(Color.BLUE);
109          } else {
110              painter.setColor(Color.BLACK);
111          }
112      }
113  
114      private void nextCol(SPainter painter,int dotSpacing){
115          painter.tl(90);
116          painter.mfd(dotSpacing);
117          painter.tr(90);
118      }
119  
120  
121  }