1 package npw; 2 3 import painter.SPainter; 4 import shapes.SRectangle; 5 import javax.swing.*; 6 import java.awt.*; 7 import java.util.Random; 8 9 10 public class Invention2 { 11 12 private void paintTheImage() { 13 //Establish the painter 14 SPainter painter = new SPainter("Invention2-The Recs", 700, 700); 15 Random randomNum = new Random(); 16 int i = 1; 17 while (i <= 200) { 18 int recH = randomNum.nextInt(200); 19 int recW = randomNum.nextInt(200); 20 21 if (recH < 75) { 22 recH = 75; 23 } 24 if (recW < 75) { 25 recW = 75; 26 } 27 28 SRectangle rec = new SRectangle(recH, recW); 29 painter.setColor(randomColor()); 30 painter.paint(rec); 31 randomlyMove (painter, randomNum); 32 i = i + 1; 33 34 } 35 } 36 37 //randomly moves/paints the recs around the canvas 38 private void randomlyMove(SPainter painter, Random randomNum) { 39 int move = randomNum.nextInt(700); 40 painter.moveToCenter(); 41 move = randomNum.nextInt(700); 42 painter.mfd(move); 43 move = randomNum.nextInt(700); 44 painter.mbk(move); 45 move = randomNum.nextInt(700); 46 painter.mrt(move); 47 move = randomNum.nextInt(700); 48 painter.mlt(move); 49 50 } 51 52 //randomizes the color that each rec is set to generating its own color combination for each one 53 private static Color randomColor() { 54 Random rgen = new Random(); 55 int r = rgen.nextInt(256); 56 int g = rgen.nextInt(256); 57 int b = rgen.nextInt(256); 58 return new Color(r, g, b); 59 60 } 61 62 //INFRASTRUCTURE 63 public Invention2() { 64 paintTheImage(); 65 } 66 67 public static void main(String[] args) { 68 SwingUtilities.invokeLater(new Runnable() { 69 @Override 70 public void run() { 71 new Invention2(); 72 } 73 }); 74 } 75 } 76