SimpleDots.java
1    /* 
2     * A program to paint an abstract gradient in the vertical 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.Random;
13   import java.util.Scanner;
14   
15   import static javax.swing.SwingUtilities.invokeLater;
16   
17   public class SimpleDots {
18        private String colorChose = getString( "What Color " );
19        private int spacingFactor = getNumber("Dot Spacing");
20        private int width = getNumber("Canvas Width in Pixels");
21        private int height = getNumber("Canvas Height in Pixels ");
22   
23       private SimpleDots() {
24           paintTheImage();
25       }
26   
27       public static void main(String[] args) {
28           invokeLater( SimpleDots::new );
29       }
30   
31       private void paintTheImage(){
32           // Establish the painter
33           SPainter painter = new SPainter("SimpleDots", width, height);
34           SCircle dot = new SCircle(5);
35   
36           // Move the painter to the upper left corner to get ready to paint the gradient
37           painter.mfd(height/2);
38           painter.tl(90);
39           painter.mfd(width/2 - 10);
40           painter.tl(90);
41   
42           // Paint it!
43           paintGradient(painter, dot, height, width, spacingFactor);
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 String getString(String prompt) {
53           String nss = JOptionPane.showInputDialog(null,prompt+"?");
54           Scanner scanner = new Scanner(nss);
55           return scanner.next();
56       }
57   
58       // Supports floating point numbers as spacingFactor values
59       private void paintGradient(SPainter painter, SCircle dot, int height, int width, int verticalSpacing){
60           // Calculate the number of columns. We want to fill the screen, but we don't want
61           // any dots only half on the canvas, so we subtract some space.
62           int nrOfCols = ( width / verticalSpacing ) - 2;
63           int columnCount = 0;
64           while (columnCount < nrOfCols){
65               nextCol(painter, verticalSpacing);
66               columnCount = columnCount + 1;
67               paintColumn(painter, dot, height);
68   
69           }
70       }
71       private void paintOneDot(SPainter painter, SCircle dot){
72           dynamicColor(painter);
73           painter.paint(dot);
74       }
75   
76       private void paintColumn(SPainter painter, SCircle dot, int height) {
77           int horizontalSpacing = spacingFactor;
78           int displacement = 0;
79   
80           while(displacement < height) {
81               displacement = displacement + horizontalSpacing;
82               painter.mfd(horizontalSpacing);
83               paintOneDot(painter, dot);
84           }
85               // Make the method invariant with respect to painter position.
86               painter.mbk(displacement);
87           }
88   
89       // Moves the painter to the next column.
90       private void nextCol(SPainter painter, double colWidth){
91           painter.tl(90);
92           painter.mfd(colWidth);
93           painter.tr(90);
94       }
95       private void dynamicColor(SPainter painter) {
96           Random rgen = new Random();
97           if (colorChose.equalsIgnoreCase( "red" )) {
98               painter.setColor( Color.RED );
99           } else if (colorChose.equalsIgnoreCase( "green" )) {
100              painter.setColor( Color.GREEN );
101          } else if (colorChose.equalsIgnoreCase( "blue" )) {
102              painter.setColor( Color.BLUE );
103          } else if (colorChose.equalsIgnoreCase( "random" )) {
104              int r = rgen.nextInt( 255 );
105              int g = rgen.nextInt( 255 );
106              int b = rgen.nextInt( 255 );
107              painter.setColor( new Color( r, g, b ) );
108          } else {
109              painter.setColor( Color.BLACK );
110          }
111      }
112  }
113