SimpleDots.java
1    /* 
2     * A program to paint simple dots that are all the same colors. 
3     */
4    
5    package npw;
6    import painter.SPainter;
7    import  shapes.SCircle;
8    import javax.swing.*;
9    import java.awt.*;
10   import java.util.Locale;
11   import java.util.Objects;
12   import java.util.Random;
13   import java.util.Scanner;
14   
15   public class SimpleDots {
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           int width = getNumber("Width");
26           int height = getNumber("Height");
27           int dotSpace = getNumber("Horizontal space between dots");
28           Color color = getColor("Color of Dots ");
29           SPainter painter = new SPainter("SimpleDots", width, height);
30           painter.setColor(color);
31           SCircle dot = new SCircle(5);
32           painter.mfd(height / 2.0);
33           painter.tl();
34           painter.mfd(width / 2.0);
35           painter.tl();
36           paintGradient(painter, dot, height, width, dotSpace);
37       }
38   
39       private static int getNumber(String prompt) {
40           String nss = JOptionPane.showInputDialog(null, prompt + "?");
41           Scanner scanner = new Scanner(nss);
42           return scanner.nextInt();
43       }
44   
45       private static Color getColor(String prompt) {
46           String command = JOptionPane.showInputDialog(null, prompt +"?");
47           Scanner scanner = new Scanner(command);
48           String ch = scanner.nextLine();
49           System.out.println(ch);
50   
51           while(true) {
52               if (command.equalsIgnoreCase("red")) {
53                   Color color = new Color(255, 0, 0);
54                   return color;
55               }else if (command.equalsIgnoreCase("blue")) {
56                       Color color = new Color(0, 0, 255);
57                       return color;
58               } else if (command.equalsIgnoreCase("green")) {
59                   Color color = new Color(0, 255, 0);
60                   return color;
61               } else {
62                   return new Color(0,0,0);
63               }
64           }
65       }
66   
67       private void paintGradient(SPainter painter, SCircle dot, int height, int width, int dotSpace) {
68           double colWidth = dot.diameter() + dotSpace;
69           int nrOfCols = (int) Math.floor((width / colWidth)) - 1;
70   
71           int cols = 0;
72           while (cols < nrOfCols) {
73               nextCol(painter, dot, dotSpace);
74               paintColumn(painter, dot, height, dotSpace);
75               cols = cols + 1;
76           }
77       }
78   
79       private void paintOneDot(SPainter painter, SCircle dot) {
80           painter.paint(dot);
81       }
82   
83       private void paintColumn(SPainter painter, SCircle dot, int height, int dotSpace) {
84           int totalDistanceTraveled = 0;
85   
86           while (totalDistanceTraveled < height - (dot.radius() * 3)) {
87               int travel = (int) (dot.diameter() + dotSpace);
88               totalDistanceTraveled = totalDistanceTraveled + travel;
89               painter.mfd(travel);
90               paintOneDot(painter, dot);
91           }
92           painter.mbk(totalDistanceTraveled);
93       }
94   
95       private void nextCol(SPainter painter, SCircle dot, int dotSpace) {
96           painter.tl();
97           painter.mfd(dot.diameter() + dotSpace);
98           painter.tr();
99       }
100  }