HirstDots.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   public class HirstDots {
12   
13       public static void main(String[] args) {
14           SwingUtilities.invokeLater(new Runnable() {
15               public void run() {
16                   new HirstDots();
17               }
18           });
19       }
20   
21   
22       public HirstDots() {
23           paintTheImage();
24       }
25   
26       public void paintTheImage() {
27           // Grab the input information
28           int width = getNumber("width");
29           int height = getNumber("height");
30           int dotSpacing = getNumber("column width");
31           // Establish the painter
32           SPainter painter= new SPainter("Hirst Dots", width, height);
33           SCircle dot = new SCircle(5);
34   
35           // Move the painter to the upper left corner to get ready to paint the gradient
36           painter.mfd(height/2);
37           painter.tl(90);
38           painter.mfd(width/2 - 10);
39           painter.tl(90);
40   
41           // Paint it!
42           paintGradient(painter, dot, height, width, dotSpacing);
43       }
44   
45       private static int getNumber(String prompt) {
46           String nss = JOptionPane.showInputDialog(null,prompt+"?");
47           Scanner scanner = new Scanner(nss);
48           return scanner.nextInt();
49       }
50   
51   
52       private void paintGradient(SPainter painter, SCircle dot, int height, int width, int dotSpacing) {
53           int cols = 0;
54           // Calculate the number of columns. We want to fill the screen but we don't want
55           //any of the dots only half on the canvas, so we subtract some space.
56           int nrOfCols = ( width / dotSpacing) - 2;
57   
58           while (cols < nrOfCols){
59               nextCol(painter, dotSpacing);
60               paintColumn(painter, dot, height, dotSpacing);
61               cols = cols + 1;
62   
63           }
64       }
65   
66   
67       private void paintOneDot(SPainter painter, SCircle dot){
68           randomColor(painter);
69           painter.paint(dot);
70   
71       }
72   
73   
74   
75       // Dots are spaced tighter together near the bottom of the canvas.
76       private void paintColumn(SPainter painter, SCircle dot, int height, int dotSpacing) {
77           int travel = 0;
78           int totalDistanceTraveled = 0;
79   
80           // Paint a row with decreasing distance between dots.
81           while(totalDistanceTraveled < height - (dot.radius() * 3)) {
82               travel = dotSpacing;
83               totalDistanceTraveled = totalDistanceTraveled + travel;
84               painter.mfd(travel);
85               paintOneDot(painter, dot);
86           }
87   
88           // Make the method invariant with respect to painter position.
89           painter.mbk(totalDistanceTraveled);
90   
91       }
92   
93       // Moves the painter to the next column.
94       private void nextCol(SPainter painter, int dotSpacing){
95           painter.tl(90);
96           painter.mfd(dotSpacing);
97           painter.tr(90);
98       }
99       private int randomDistance(int maxDistance){
100          Random rgen = new Random();
101          return rgen.nextInt(maxDistance);
102      }
103      private void randomColor(SPainter painter) {
104          Random rgen = new Random();
105          int r = rgen.nextInt(255);
106          int g = rgen.nextInt(255);
107          int b = rgen.nextInt(255);
108          painter.setColor(new Color(r, g, b));
109      }
110  
111  }