Invention2.java
1    /* 
2     *program to create a visually interesting canvas with an if and while statement that is different each time. 
3     */
4    package npw;
5    
6    import painter.SPainter;
7    import shapes.SRectangle;
8    import java.awt.Color;
9    import javax.swing.SwingUtilities;
10   
11   public class Invention2 {
12   
13       private void paintTheImage() {
14           SPainter Dylan = new SPainter("Invention2", 750, 700);
15           SRectangle rectangle = new SRectangle(250,150);
16           Dylan.move();
17   
18           int i = 1;
19           while (i < 16) {
20               if (i == 1) {
21                   Dylan.setColor(randomColor());
22               } else if (i==2) {
23                   Dylan.setColor(Color.pink);
24               } else if (i==3) {
25                   Dylan.setColor(randomColor());
26               } else if (i==4) {
27                   Dylan.setColor(Color.ORANGE);
28               } else if (i==5) {
29                   Dylan.setColor(randomColor());
30               } else if (i==6) {
31                   Dylan.setColor(Color.YELLOW);
32               } else if (i==7) {
33                   Dylan.setColor(randomColor());
34               } else if (i==8) {
35                   Dylan.setColor(Color.black);
36               } else if (i==9) {
37                   Dylan.setColor(randomColor());
38               } else if (i==10) {
39                   Dylan.setColor(Color.blue);
40               } else if (i==11) {
41                   Dylan.setColor(randomColor());
42               } else if (i==12) {
43                   Dylan.setColor(Color.GREEN);
44               } else if (i==13) {
45                   Dylan.setColor(randomColor());
46               } else if (i==14) {
47                   Dylan.setColor(Color.RED);
48               } else if (i==15) {
49                   Dylan.setColor(randomColor());
50               }
51               Dylan.paint(rectangle);
52               Dylan.setColor(Color.BLACK);
53               Dylan.draw(rectangle);
54               Dylan.move();
55               i = i + 1;
56           }
57       }
58   
59       private static Color randomColor() {
60           int r = (int)(Math.random()*256);
61           int g = (int)(Math.random()*256);
62           int b = (int)(Math.random()*256);
63           return new Color(r,g,b);
64       }
65   
66       public Invention2() {
67           paintTheImage();
68       }
69          public static void main(String[] args) {
70           SwingUtilities.invokeLater(new Runnable() {
71               public void run() {
72                   new Invention2();
73                   }
74              });
75       }
76   
77   }