1 package npw; 2 3 import painter.SPainter; 4 import shapes.SRectangle; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.util.Random; 9 10 public class Invention2 { 11 public Invention2() { 12 paintTheImage(); 13 } 14 15 private void paintTheImage() { 16 // Creates the rectangle 17 SRectangle rec = new SRectangle (250, 400); 18 19 // Creates tab that image will be displayed in 20 SPainter painter = new SPainter("Invention 2", 800,800); 21 22 // Paints Rectangles 23 paintRectangles(rec, painter); 24 } 25 26 private void paintRectangles(SRectangle rec, SPainter painter){ 27 int h = 250; 28 int w = 500; 29 while (w > 0) { 30 painter.move(); 31 painter.setColor(randomColor()); 32 painter.paint(rec); 33 w = w - 30; 34 h = h - 50; 35 if (h < 0) { 36 h = h + 300; 37 } 38 } 39 } 40 41 private static Color randomColor () { 42 Random rgen = new Random(); 43 int r = rgen.nextInt(256); 44 int g = rgen.nextInt(256); 45 int b = rgen.nextInt(256); 46 return new Color(r, g, b); 47 } 48 49 // Every program needs a main method 50 public static void main (String[] args) { 51 SwingUtilities.invokeLater(new Runnable() { 52 public void run() { new Invention2(); } 53 }); 54 } 55 } 56