Invention2.java
1    package npw;
2    
3    /* 
4     *Paint an image subject to some given constraints 
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", 1000, 1000);
18           Random rand = new Random();
19           //double rand2 = Math.random();
20           SRectangle rect = new SRectangle(rand.nextInt(100),rand.nextInt(100));
21           painter.setColor(Color.BLACK);
22           int i = 0;
23           while (i < 5){
24               if(painter.color==Color.BLACK){
25                   painter.paint(rect);
26                   painter.setColor(Color.BLUE);
27               }
28               if(painter.color==Color.BLUE){
29                   rect.setWidth(rand.nextInt(100));
30                   rect.setHeight(rand.nextInt(100));
31                   painter.mfd(rect.height());
32                   painter.paint(rect);
33                   painter.setColor(Color.RED);
34               }
35               if(painter.color==Color.RED){
36                   rect.setWidth(rand.nextInt(100));
37                   rect.setHeight(rand.nextInt(100));
38                   painter.paint(rect);
39                   painter.mfd(rect.height());
40                   painter.setColor(Color.GREEN);
41   
42               }
43               if(painter.color==Color.GREEN){
44                   rect.setWidth(rand.nextInt(100));
45                   rect.setHeight(rand.nextInt(100));
46                   painter.mfd(rect.height());
47                   painter.paint(rect);
48               }
49   
50               else{
51                   painter.mfd(20);
52                   painter.setColor(Color.RED);
53                   painter.paint(rect);
54               }
55               i++;
56           }
57       }
58   
59       public Invention2(){
60           paintTheImage();
61       }
62   
63       public static void main(String[] args){
64           SwingUtilities.invokeLater(new Runnable(){
65               public void run(){
66                   new Invention2();
67               }
68           });
69       }
70   
71   }
72