1 /* 2 * Assignment 4 Problem 7: Nondeterministic Invention 3 * It uses at least one while statement in a nontrivial way. 4 * It uses at least one if statement in a nontrivial way. 5 * It features solely rectangles, all generated from a single SRectangle. 6 * The program takes no input from the user of the program. 7 * It creates a different image each time the program is executed, different with respect to number or size or color or whatever of the featured object. 8 * There is some chance that the casual observer might find the image interesting! 9 */ 10 11 package npw; 12 13 import painter.SPainter; 14 import shapes.SRectangle; 15 import javax.swing.*; 16 import java.awt.*; 17 import java.util.Random; 18 19 public class Invention2 { 20 public static void main(String[] args){ 21 SwingUtilities.invokeLater(new Runnable() { 22 public void run() { 23 new Invention2(); 24 } 25 }); 26 } 27 28 public Invention2() { 29 paintTheImage(); 30 } 31 32 private void paintTheImage() { 33 SPainter painter = new SPainter("Optical Illusion",800,800); 34 SRectangle rectangle = new SRectangle(799,800); 35 painter.setColor(Color.BLACK); 36 painter.paint(rectangle); 37 int i = 0; 38 // set the limit of rectangle to 110 so that the latest rectangles will not become big again. 39 while (i <= 110){ 40 // for aesthetic: 41 // if even, random color will be chosen and painted. 42 // if even, the square will tilt left to random degree. 43 if (i % 2 == 0) { 44 painter.tl(randomNumber()); 45 rectangle.shrink(randomNumber(), randomNumber()); 46 painter.setColor(randomColor()); 47 painter.paint(rectangle); 48 painter.setColor(Color.BLACK); 49 painter.draw(rectangle); 50 } else { 51 // For aesthetic: 52 // if odd, the square will be black and tilt left only 10 degree 53 // so that the final product be be more spiral. 54 painter.tl(10); 55 rectangle.shrink(10, 10); 56 painter.setColor(Color.BLACK); 57 painter.paint(rectangle); 58 } 59 i++; 60 } 61 } 62 63 private static double randomNumber() { 64 Random rgen = new Random(); 65 int degree = rgen.nextInt(10); 66 return degree; 67 } 68 69 private static Color randomColor() { 70 Random rgen = new Random(); 71 int r = rgen.nextInt(256); 72 int g = rgen.nextInt(256); 73 int b = rgen.nextInt(256); 74 return new Color (r,g,b); 75 } 76 } 77