/home/kchan2/NetBeansProjects/CS1/src/npw/Invention2.java |
1
2
3
4
5
6
7
8
9 package npw;
10
11 import shapes.SRectangle;
12 import painter.SPainter;
13 import java.awt.Color;
14 import java.util.Random;
15
16
17
18 @author
19
20 public class Invention2 {
21
22
23 @param args
24
25 public static void main(String[] args) {
26 SPainter painter = new SPainter("Invention 2", 600, 600);
27 painter.setBrushWidth(5);
28 SRectangle rectangle = new SRectangle(400,300);
29 paintTheImage(painter,rectangle);
30 }
31
32 private static void paintTheImage(SPainter painter, SRectangle rectangle) {
33 int i = 1;
34 while ( i <= 20 ) {
35 paintTwoRectangle(painter,rectangle);
36 i = i + 1 ;
37 }
38 }
39
40 private static void paintTwoRectangle(SPainter painter, SRectangle rectangle) {
41 Random rgen = new Random();
42 double length = rgen.nextInt(300);
43 double width = rgen.nextInt(200);
44 if (length > 250) {
45 painter.setColor(Color.BLUE);
46 } else if (length < 50){
47 painter.setColor(Color.YELLOW);
48 } else painter.setColor(randomColor());
49 rectangle.shrink(length,width);
50 painter.draw(rectangle);
51 rectangle.shrink(-length/7*6,-width/7*6);
52 painter.draw(rectangle);
53 }
54
55 private static Color randomColor() {
56 int rv = (int)(Math.random()*256);
57 int gv = (int)(Math.random()*256);
58 int bv = (int)(Math.random()*256);
59 Color color = new Color(rv,gv,bv);
60 return color;
61 }
62
63 }
64