Invention2.java
1    /* 
2    It uses at least one while statement in a nontrivial way. 
3    It uses at least one if statement in a nontrivial way. 
4    It features solely rectangles, all generated from a single rectangle. 
5    The program takes no input from the user of the program. 
6    It creates a different image each time the program is executed, different with respect to number or size or color or whatever of the featured object. 
7    There is some chance that the casual observer might find the image interesting! 
8     */
9    package NPW;
10   
11   import java.awt.*;
12   import java.util.Random;
13   import java.util.Scanner;
14   import javax.swing.*;
15   
16   import painter.SPainter;
17   import shapes.SCircle;
18   import shapes.SRectangle;
19   import shapes.SSquare;
20   
21   public class Invention2 {
22   
23       private void paintTheImage() {
24           //measurements
25           int squareHeight = 60;
26           int squareWidth = 40;
27           int nrOfRectangles = 30;
28   
29           //Establish Painter
30           int shrink = 50;
31           SPainter painter = new SPainter("Invention2", 500, 500);
32           painter.setBrushWidth(4);
33           SRectangle rectangle = new SRectangle(squareHeight,squareWidth);
34   
35           //make rectangles
36           paintRectangles(painter,rectangle,nrOfRectangles, shrink);
37       }
38   
39       private static void paintRectangles(SPainter painter,SRectangle rectangle, int nrOfRectangles,
40                                           int shrink) {
41           int i = 0;
42           while (i <= nrOfRectangles ) {
43               painter.setColor(randomColor());
44               painter.move();
45               painter.paint(rectangle);
46               i = i + 1;
47           }
48           if (i < 20) {
49               rectangle.shrink(shrink,shrink);
50   
51           }
52   
53       }
54   
55       private static Color randomColor() {
56           Random rgen = new Random();
57           int r = rgen.nextInt(256);
58           int g = rgen.nextInt(256);
59           int b = rgen.nextInt(256);
60           return new Color(r,b,g);
61       }
62   
63       public Invention2() {paintTheImage();}
64   
65       public static void main(String[] args){
66           SwingUtilities.invokeLater(new Runnable() {
67               @Override
68               public void run() {
69                   new Invention2();
70               }
71           });
72       }
73   }
74