/home/ffrigin/NetBeansProjects/CS1/src/npw/Invention2.java
 1 /*
 2   * A program that uses at least one while and one if statement, while only 
 3   * using rectangles to create a different image each time.
 4  */
 5 package npw;
 6 
 7 import java.awt.Color;
 8 import java.util.Random;
 9 import javax.swing.SwingUtilities;
10 import painter.SPainter;
11 import shapes.SRectangle;
12 
13 /**
14  *
15  * @author ffrigin
16  */
17 public class Invention2 {
18 
19     // REQUIRED INFRASTRUCTURE
20     public Invention2() {
21         paintTheImage();
22     }
23 
24     public static void main(String[] args) {
25         SwingUtilities.invokeLater(new Runnable() {
26             public void run() {
27                 new Invention2();
28             }
29         });
30     }
31 
32     private void paintTheImage() {
33         SPainter painter = new SPainter("Invention2", 800, 800);
34         int numOfRectangles = 75;
35         paintRectangles(painter, numOfRectangles); 
36     }
37 
38     private void paintRectangles(SPainter painter, int numOfRectangles) {
39         int i = 1;
40         while (i <= numOfRectangles) {
41             paintOneRectangle(painter); 
42             i = i + 1;
43         }
44     }
45 
46     private void paintOneRectangle(SPainter painter) {
47         Random rgen = new Random();
48         int rn = rgen.nextInt(3);
49         if (rn == 0) {
50             painter.setColor(Color.blue);
51         } else if (rn == 1) {
52             painter.setColor(Color.orange);
53         } else {
54             painter.setColor(Color.white);
55         }
56         painter.move();
57         SRectangle rectangle = new SRectangle(50, 100);
58         painter.paint(rectangle);
59         painter.setColor(randomColor());
60         painter.draw(rectangle);
61     }
62 
63     private static Color randomColor() {
64         int rv = (int) (Math.random() * 256);
65         int gv = (int) (Math.random() * 256);
66         int bv = (int) (Math.random() * 256);
67         return new Color(rv, gv, bv);
68 
69     }
70 }
71