AbstractGradient.java
1    /* 
2    * A program to paint an abstract gradient in the vertical direction 
3     */
4    package assign_4;
5    
6    import painter.SPainter;
7    import shapes.SCircle;
8    
9    import javax.swing.*;
10   import java.awt.*;
11   import java.util.Random;
12   import java.util.Scanner;
13   
14   public class AbstractGradient {
15       public static void main(String[] args){
16           SwingUtilities.invokeLater(new Runnable() {
17               @Override
18               public void run() {
19                   new AbstractGradient();
20               }
21           });
22       }
23       public AbstractGradient() {
24           paintTheImage();
25       }
26       private void paintTheImage(){
27           //Grab the input information
28           int width = getNumber("width");
29           int height = getNumber("height");
30           int colWidth = getNumber("column width");
31           //Establish the painter
32           SPainter painter = new SPainter("Abstract Gradient", width, height);
33           SCircle dot = new SCircle(5);
34           //Move the painter to the upper left corner to get ready to paint the gradient
35           painter.mfd(height/2);
36           painter.tl(90);
37           painter.mfd(width/2 - 10);
38           painter.tl(90);
39           //Paint it!
40           paintGradient(painter, dot, height, width, colWidth);
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 colWidth){
48           int cols = 0;
49           //Calculate the number of columns. We want to fill the screen, but we don't want
50           //any dots only half on the canvas, so we subtract some space
51           int nrOfCols = (width/colWidth) - 2;
52           while(cols<nrOfCols){
53               nextCol(painter, colWidth);
54               paintColumn(painter, dot, height);
55               cols = cols + 1;
56           }
57       }
58       private void paintOneDot(SPainter painter, SCircle dot){
59           painter.setColor(Color.black);
60           painter.paint(dot);
61       }
62       //Dots are spaced tighter together near the bottom of the canvas
63       private void paintColumn(SPainter painter, SCircle dot, int height){
64           int travel = 0;
65           int totalDistanceTraveled = 0;
66           //Paint a row with decreasing distance between dots
67           while(totalDistanceTraveled < height - (dot.radius() * 3)){
68               travel = randomDistance((height - totalDistanceTraveled) / 4);
69               totalDistanceTraveled = totalDistanceTraveled + travel;
70               painter.mfd(travel);
71               paintOneDot(painter,dot);
72           }
73           //Make the method invariant with respect to painter position
74           painter.mbk(totalDistanceTraveled);
75       }
76       //Moves the painter to the next column
77       private void nextCol(SPainter painter, int colWidth){
78           painter.tl(90);
79           painter.mfd(colWidth);
80           painter.tr(90);
81       }
82       private int randomDistance(int maxDistance){
83           Random rgen = new Random();
84           return rgen.nextInt(maxDistance);
85       }
86   }
87