1 /* 2 * A program that uses one rectangle, a while statment, and an if statement. A different image is created each time it is run. 3 */ 4 5 package npw; 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 //REQUIRED INFRASTRUCTURE 17 18 public Invention2() { 19 paintTheImage(); 20 } 21 22 public static void main(String[] args) { 23 SwingUtilities.invokeLater(new Runnable() { 24 public void run() { 25 new Invention2(); 26 } 27 }); 28 } 29 30 31 32 private void paintTheImage() { 33 SPainter painter = new SPainter("Invention2", 600, 600); 34 int nrOfRectangles = 1000; 35 paintRectangles(painter,nrOfRectangles); // 36 } 37 38 39 private void paintRectangles(SPainter painter, int nrOfRectangles) { 40 int i = 1; 41 while ( i <= nrOfRectangles) { 42 paintOneRectangle(painter); 43 i = i + 1; 44 } 45 } 46 47 private void paintOneRectangle(SPainter painter) { 48 Random rgen = new Random(); 49 int rn = rgen.nextInt(3); 50 if (rn == 0 ) { 51 painter.setColor(Color.PINK); 52 } else if (rn ==1) { 53 painter.setColor(Color.GREEN); 54 } else { 55 painter.setColor(Color.BLACK); 56 } 57 painter.move(); 58 SRectangle rectangle = new SRectangle(20, 50); 59 painter.paint(rectangle); 60 painter.setColor(Color.GRAY); 61 painter.draw(rectangle); 62 } 63 } 64