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