SimpleDots.java
1    /* 
2     * A program to paint an abstract gradient in the vertical and horizontal direction. 
3     */
4    
5    package npw;
6    
7    import painter.SPainter;
8    import shapes.SCircle;
9    
10   import javax.swing.*;
11   import java.awt.*;
12   import java.util.Scanner;
13   
14   public class SimpleDots {
15   
16       public static void main(String[] args) {
17           SwingUtilities.invokeLater(SimpleDots::new);
18       }
19   
20       public SimpleDots() {
21           paintTheImage();
22       }
23   
24       private void paintTheImage() {
25           // Grab the input information
26           int width = getNumber("width");
27           int height = getNumber("height");
28           int horizontalSpace = getNumber("horizontal space between dots");
29           int verticalSpace = getNumber("vertical space between dots");
30           Color color = pickColor();
31           // Establish the painter
32           SPainter painter = new SPainter("Simple Dots" ,  width , height);
33           SCircle dot = new SCircle(5);
34   
35   
36           // Move the painter to the upper left corner and get ready to paint the gradient
37           painter.mfd(height/2.0);
38           painter.tl();
39           painter.mfd(width/2.0);
40           painter.tl();
41   
42           // Paint it!
43           paintGradient(painter, dot, height, width, horizontalSpace, verticalSpace, color);
44       }
45   
46       private static int getNumber(String prompt) {
47           String nss = JOptionPane.showInputDialog(null, prompt+"?");
48           Scanner scanner = new Scanner(nss);
49           return scanner.nextInt();
50       }
51   
52       private static Color pickColor() {
53           String nss = JOptionPane.showInputDialog(null, "Pick Color");
54           Scanner scanner = new Scanner(nss);
55           String color = scanner.next();
56           if (color.equalsIgnoreCase("red")) {
57               return Color.RED;
58           }
59           if (color.equalsIgnoreCase("blue")) {
60               return Color.BLUE;
61           }
62           if (color.equalsIgnoreCase("green")) {
63               return Color.GREEN;
64           } else {
65               return Color.BLACK;
66           }
67       }
68   
69       private void paintGradient(SPainter painter, SCircle dot, int height, int width, int horizontalSpace, int verticalSpace, Color color){
70           // Calculate the number of columns. We want to fill the screen, but don't want any columns half on the canvas.
71           // A column takes up the horizontal space of a dot's diameter plus the space between it and a neighbor.
72           double colWidth = dot.diameter() + horizontalSpace;
73           double rowWidth = dot.diameter() + verticalSpace;
74           // We don't want a column all the way on the edge on the right side, so subtract 1.
75           int nrOfCols = (int) Math.floor(( width / colWidth )) -1;
76           int nrOfRows = (int) Math.floor(( width / rowWidth )) -1;
77   
78           int cols = 0;
79           while (cols < nrOfCols) {
80               nextCol(painter, dot, horizontalSpace);
81               paintColumn(painter, dot, height, horizontalSpace, color);
82               cols = cols + 1;
83           }
84           int rows = 0;
85           while ((rows > nrOfRows)) {
86               nextRow(painter, dot, verticalSpace);
87               paintRow(painter, dot, height, verticalSpace, color);
88               rows = rows + 1;
89           }
90       }
91   
92       private void paintOneDot(SPainter painter, SCircle dot, Color color) {
93           painter.setColor(color);
94           painter.paint(dot);
95       }
96   
97       // Dots are spaced more tightly together near the bottom of the canvas.
98       private void paintColumn(SPainter painter, SCircle dot, int height, int horizontalSpace, Color color) {
99           int totalDistanceTraveled = 0;
100  
101          // Paint a column with decreasing distance between dots.
102          while (totalDistanceTraveled < height - (dot.radius() *3)) {
103              int travel = horizontalSpace -1;
104              totalDistanceTraveled = totalDistanceTraveled + travel;
105              painter.mfd(travel);
106              paintOneDot(painter, dot, color);
107          }
108  
109          // Make the method invariant with respect to painter position.
110          painter.mbk(totalDistanceTraveled);
111      }
112  
113      private void paintRow(SPainter painter, SCircle dot, int height, int verticalSpace, Color color) {
114          int totalDistanceTraveled = 0;
115          while (totalDistanceTraveled < height - (dot.radius() *3)) {
116              int travel = verticalSpace -1;
117              totalDistanceTraveled = totalDistanceTraveled + travel;
118              painter.mfd(travel);
119              paintOneDot(painter, dot, color);
120          }
121      }
122      // Moves the painter to the next column.
123      private void nextCol(SPainter painter, SCircle dot, int horizontalSpace) {
124          painter.tl();
125          painter.mfd(dot.diameter() + horizontalSpace);
126          painter.tr();
127      }
128      private void nextRow(SPainter painter, SCircle dot, int verticalSpace) {
129          painter.tl();
130          painter.mfd(dot.diameter() + verticalSpace);
131          painter.tr();
132      }
133  }
134  
135  
136