Invention2.java
1    package npw;/* 
2     *Project invention1 
3     * In this program, the user will have to solve the traffic problem by changing traffic light, in other words this is a 
4     * program which you can use to change the lights. 
5     */
6    
7    import painter.SPainter;
8    import shapes.SRectangle;
9    
10   import javax.swing.*;
11   import java.awt.*;
12   import java.util.Random;
13   
14   public class Invention2 {
15   
16           private void paintTheImage() {
17               SPainter painter = new SPainter("Invention2",800,800);
18               painter.setBrushWidth(2);
19               SRectangle rec = new SRectangle(50,20);
20               int distance1 = 25;
21               double distance = (2*distance1)+rec.height();
22               int rotate = 40 ;
23               double movingFactor = 2.5;
24               double removingFactor= movingFactor/2;
25               Color color1 = randomColor1();
26               Color color2 = randomColor2();
27               paintOneRec(painter,rec,distance,rotate,color1,color2,movingFactor,removingFactor);
28   
29   
30           }
31           private void paintOneRec(SPainter painter, SRectangle rec, double distance, int rotate, Color color1, Color color2, double movingFactore, double removingFactor){
32              painter.mbk(distance);
33              painter.setColor(color1);
34              painter.paint(rec);
35              painter.setColor(Color.BLACK);
36              painter.draw(rec);
37              painter.mfd(distance);
38   
39              int i = randomNr();
40               while(i>0) {
41                   if(i%2==0){
42                   painter.tr(rotate);
43                   painter.mbk(distance+0.5);
44                   painter.mlt(movingFactore);
45                   painter.setColor(color1);
46                   painter.paint(rec);
47                   painter.setColor(Color.BLACK);
48                   painter.draw(rec);
49                   painter.mfd(distance);
50                   painter.mrt(removingFactor);
51                   }else {
52                       painter.tr(rotate);
53                       painter.mbk(distance+0.5);
54                       painter.mlt(movingFactore);
55                       painter.setColor(color2);
56                       painter.paint(rec);
57                       painter.setColor(Color.BLACK);
58                       painter.draw(rec);
59                       painter.mfd(distance);
60                       painter.mrt(removingFactor);
61                   }
62   
63                   i = i -1;
64                   movingFactore = movingFactore+0.5;
65                   removingFactor = removingFactor/2;
66   
67               }
68           }
69   
70           private int randomNr(){
71               Random rgen = new Random();
72               int nrOfRec = rgen.nextInt(5000);
73                return nrOfRec;
74           }
75   
76   
77           private Color randomColor1(){
78               int r = (int) (Math.random()*256);
79               int b = (int) (Math.random()*256);
80               int g = (int) (Math.random()*256);
81   
82               return new Color(r,b,g);
83           }
84           private Color randomColor2(){
85               int r =(int) (Math.random()*256);
86               int b =(int) (Math.random()*256);
87               int g =(int) (Math.random()*256);
88   
89               return new Color(r,b,g);
90           }
91   
92           public Invention2() {
93               this.paintTheImage();
94           }
95   
96           public static void main(String[] args) {
97               SwingUtilities.invokeLater(new Runnable() {
98                   public void run() {
99                       new Invention2();
100                  }
101              });
102          }
103      }
104  
105