Invention2.java
1    package npw;
2    
3    import painter.SPainter;
4    import shapes.SRectangle;
5    
6    import javax.swing.*;
7    import java.awt.*;
8    import java.awt.geom.Point2D;
9    import java.util.Random;
10   
11   public class Invention2 {
12   
13       Random r = new Random();
14   
15   
16       int canvasSide = 800;
17       int numberOfRectangles = 25;
18   
19   
20       public static void main(String[] args) {
21           SwingUtilities.invokeLater(new Runnable() {
22               public void run() {
23                   new Invention2();
24               }
25           });
26       }
27       //conductor
28       public Invention2() {
29           paintTheImage();
30       }
31   
32       private void paintTheImage (){
33           SPainter painter = new SPainter ("Invention 2",  canvasSide, canvasSide);
34           SRectangle rectangle = new SRectangle(r.nextInt(500), r.nextInt(200));
35           int count = 0;
36           while (count< numberOfRectangles){
37               randomPosition(painter);
38               randomSize(painter, rectangle);
39               count = 1 + count;
40           }
41   
42       }
43   
44   
45   
46       private Color randomColor() {
47           int y = r.nextInt(256);
48           int o = r.nextInt(256);
49           int b = r.nextInt(256);
50           return new Color(y, o, b);
51       }
52   
53       private void randomSize(SPainter painter, SRectangle rectangle) {
54           //generate size randomly (true or false value)
55           if (r.nextBoolean()) {
56               int minimumWidth = 60;
57               int minimumHeight= 50;
58               double Width = rectangle.width()/2;
59               double height = rectangle.height()/2;
60   
61               if (Width< minimumWidth){
62                   Width = minimumWidth;
63   
64               }
65   
66               if (height < minimumHeight){
67                   height = minimumHeight;
68               }
69   
70               rectangle.shrink(height, Width);
71   
72           } else {
73   
74               int maxWidth = 500;
75               int maxHeight= 550;
76               double Width = Math.abs(rectangle.width()*2);
77               double height = Math.abs(rectangle.height()*2);
78   
79               if (Width> maxWidth){
80                   Width = 0;
81   
82               }
83   
84               if (height > maxHeight){
85                   height = 0;
86               }
87   
88               rectangle.expand(height, Width);
89   
90           }
91   
92           painter.setColor(randomColor());
93           painter.paint(rectangle);
94       }
95   
96       private void  randomPosition(SPainter painter){
97           //takes in position object (x,y)
98           painter.setPosition(new Point2D.Double(r.nextInt(canvasSide),r.nextInt(canvasSide)));
99       }
100  
101  }