Invention2.java
1    package npw;
2    
3    import painter.SPainter;
4    import shapes.SRectangle;
5    
6    import javax.swing.*;
7    import java.awt.*;
8    
9    public class Invention2 {
10   
11       public static void main(String[] args) {
12           SwingUtilities.invokeLater(new Runnable() {
13               public void run() {
14                   new Invention2();
15               }
16           });
17       }
18   
19       public Invention2() {
20           paintTheImage();
21       }
22   
23       private void paintTheImage() {
24           int width = 800;
25   
26           SPainter painter = new SPainter("Invention2", width, width);
27           paintRectangles(width, painter);
28   
29   
30       }
31   
32       private void paintRectangles(int width, SPainter painter) {
33           int i = width;
34           while (i > 0) {
35               SRectangle rect = new SRectangle(i, i/2);
36               painter.setColor(randomColor(i));
37               painter.paint(rect);
38               SRectangle rect2 = new SRectangle (i / 4, i);
39               painter.setColor(randomColor(i));
40               painter.paint(rect);
41               i = i - 50;
42   
43           }
44       }
45       private static Color randomColor(int i){
46           int rv = (int)(Math.random()*256);
47           int gv = (int)(Math.random()*256);
48           int bv = (int)(Math.random()*256);
49   
50           if ( i >= 600){
51               return new Color(rv,0,0);
52           }
53           else if (600>i && i>300){
54               return new Color (0,gv,0);
55           }
56           else{
57               return new Color (0,0,bv);
58           }
59   
60       }
61   
62   }