1 /* 2 * This programs creates randomly sized and colored concentric rectangles. 3 */ 4 5 package npw; 6 7 import painter.SPainter; 8 import shapes.SRectangle; 9 import javax.swing.*; 10 import java.awt.*; 11 import java.util.Random; 12 13 public class Invention2 { 14 15 private void paintTheImage(){ 16 //Variables 17 int canvaswidth = 800; 18 int recSide = ((canvaswidth- 100)/2); 19 int numOfRec = randomNum(); 20 int sizeFx = recSide / numOfRec; 21 int sizeFx2 = canvaswidth / numOfRec; 22 int i = 0; 23 SPainter painter = new SPainter("Invention2", 800, 400 ); 24 SRectangle rectangle = new SRectangle(recSide, canvaswidth ); 25 26 while (i < numOfRec) { 27 if (i % 2 > 0) { 28 painter.setColor(randomColor()); 29 painter.paint(rectangle); 30 rectangle.shrink(sizeFx, sizeFx2); 31 } else { 32 painter.setColor(randomColor()); 33 painter.paint(rectangle); 34 rectangle.shrink(sizeFx, sizeFx2); 35 } 36 i++; 37 } 38 39 40 } 41 42 private static Color randomColor() { 43 Random rgen = new Random(); 44 int r = rgen.nextInt(256); 45 int g = rgen.nextInt(256); 46 int b = rgen.nextInt(256); 47 return new Color(r,g,b); 48 49 } 50 51 52 private int randomNum() { 53 Random randNum = new Random(); 54 int n = randNum.nextInt(100); 55 n += 1; 56 return n; 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 }