HirstDots.java
1    package npw;
2    
3    import painter.SPainter;
4    import shapes.SCircle;
5    import shapes.SSquare;
6    
7    import javax.swing.*;
8    import java.awt.*;
9    import java.util.Random;
10   import java.util.Scanner;
11   
12   public class HirstDots {
13   
14       public static void main(String[] args) {
15           SwingUtilities.invokeLater(HirstDots::new);
16       }
17   
18       public HirstDots() {
19           paintTheImage();
20       }
21   
22       private void paintTheImage() {
23           // Grab the input information
24           int width = getNumber("width");
25           int height = getNumber("height");
26           int dotSpace = getNumber("horizontal space between dots");
27           // Establish the painter
28           SPainter painter = new SPainter("Abstract Gradient", width, height);
29           SCircle dot = new SCircle(5);
30   
31   
32           // Move the painter to the upper left corner to get ready to paint the gradient
33           painter.mfd(height / 2.0);
34           painter.tl();
35           painter.mfd(width / 2.0);
36           painter.tl();
37   
38           // Paint it!
39           paintGradient(painter, dot, height, width, dotSpace);
40       }
41   
42       private static int getNumber(String prompt) {
43           String nss = JOptionPane.showInputDialog(null,prompt+"?");
44           Scanner scanner = new Scanner(nss);
45           return scanner.nextInt();
46       }
47       private void paintGradient(SPainter painter, SCircle dot, int height, int width, int dotSpace) {
48           // Calculate the number of columns. We want to fill the screen, but don't want any columns half on the canvas.
49           // A column takes up the horizontal space of a dot's diameter plus the space between it and a neighbor.
50           double colWidth = dot.diameter() + dotSpace;
51           // We don't want a column all the way on the edge on the right side, so subtract 1.
52           int nrOfCols = (int) Math.floor((width / colWidth)) - 1;
53   
54           int cols = 0;
55           while (cols < nrOfCols) {
56               nextCol(painter, dot, dotSpace);
57               paintColumn(painter, dot, height, dotSpace);
58               cols = cols + 1;
59           }
60       }
61   
62       private void paintOneDot(SPainter painter, SCircle dot){
63           painter.setColor(randomColor());
64           painter.paint(dot);
65           painter.setColor(Color.BLACK);
66       }
67   
68       private Color randomColor() {
69           Random rgen = new Random();
70           int r = rgen.nextInt(256);
71           int g = rgen.nextInt(256);
72           int b = rgen.nextInt(256);
73           return new Color(r,g,b);
74       }
75   
76   
77       // Dots are spaced more tightly together near the bottom of the canvas.
78       private void paintColumn(SPainter painter, SCircle dot, int height, int dotSpace) {
79           int totalDistanceTraveled = 0;
80   
81           // Paint a column with decreasing distance between dots.
82           while (totalDistanceTraveled < height - (dot.radius() * 3)) {
83               int travel = (int) (dot.diameter() + dotSpace);
84               totalDistanceTraveled = totalDistanceTraveled + travel;
85               painter.mfd(travel);
86               paintOneDot(painter, dot);
87           }
88   
89           // Make the method invariant with respect to painter position.
90           painter.mbk(totalDistanceTraveled);
91       }
92   
93   
94       // Moves the painter to the next column.
95       private void nextCol(SPainter painter, SCircle dot, int dotSpace) {
96           painter.tl();
97           painter.mfd(dot.diameter() + dotSpace);
98           painter.tr();
99       }
100  }
101  
102