Invention2.java
1    /* 
2     * This program creates 10 randomized rectangles with 
3     * a 50% chance to paint 10 more rectangles (Stackable) 
4     */
5    
6    
7    package npw;
8    
9    import java.awt.Color;
10   import java.util.Random;
11   import javax.swing.SwingUtilities;
12   
13   import painter.SPainter;
14   import shapes.SRectangle;
15   
16   public class Invention2 {
17   
18       private void paintTheImage(){
19           // Dimension Variables
20           int width = 0;
21           int height = 0;
22           int canvasSize = 800;
23           int numRectangles = 10;
24   
25           // Object Initialization
26           SRectangle rectangle = new SRectangle(width, height);
27           SPainter klee = new SPainter( "Invention 2", canvasSize, canvasSize);
28   
29           // Paints 10 Randomized Rectangles
30               // Has a 50% chance to paint 10 more rectangles (Stackable) //
31           Random moreRectangles = new Random();
32           int chance = 1;
33           while(chance == 1){
34               beginConstruction(rectangle, klee, numRectangles);
35               chance = moreRectangles.nextInt(1, 3);
36           }
37       }
38   
39       // Moves Painter to Random Position Until 10 Rectangles are Painted
40       private void beginConstruction(SRectangle rectangle, SPainter klee, int numRectangles){
41           int i = 0;
42           while(i < numRectangles){
43               klee.move();
44               paintRectangle(rectangle, klee);
45               i++;
46           }
47       }
48   
49       // Paints a Randomized Rectangle
50       private void paintRectangle(SRectangle rectangle, SPainter klee){
51           klee.setColor(randomColor());
52           rectangle.setHeight(randomDimension());
53           rectangle.setWidth(randomDimension());
54           klee.paint(rectangle);
55       }
56   
57       // Returns a Random Dimension for Rectangles
58       private static int randomDimension() {
59           Random random = new Random();
60           int dimension = random.nextInt(400);
61           return dimension;
62       }
63   
64       // Returns a Random Color
65       private static Color randomColor() {
66           Random random = new Random();
67           int r = random.nextInt(256);
68           int g = random.nextInt(256);
69           int b = random.nextInt(256);
70           return new Color(r,b,g);
71       }
72   
73   
74       // REQUIRED INFRASTRUCTURE //
75       public Invention2(){
76           paintTheImage();
77       }
78   
79       public static void main(String[] args){
80           SwingUtilities.invokeLater(new Runnable() {
81               @Override
82               public void run() {
83                   new Invention2();
84               }
85           });
86       }
87   }
88