1 /* 2 * Program to paint an image subject to set constraints. 3 */ 4 5 package npw; 6 7 import java.awt.*; 8 import javax.swing.SwingUtilities; 9 import java.util.Random; 10 import painter.SPainter; 11 import shapes.SRectangle; 12 13 public class Invention2 { 14 15 private void paintTheImage() { 16 SPainter painter = new SPainter("Invention 2", 700, 700); 17 Random randomNum = new Random(); 18 painter.setBrushWidth(2); 19 int i = 1; 20 while (i <= 100) { 21 int recHeight = randomNum.nextInt(200); 22 int recWidth = randomNum.nextInt(200); 23 if (recHeight < 50) { 24 recHeight = 50; 25 } 26 if (recWidth < 50) { 27 recWidth = 50; 28 } 29 SRectangle rec = new SRectangle(recHeight, recWidth); 30 painter.setColor(randomColor()); 31 painter.paint(rec); 32 painter.setColor(Color.RED); 33 painter.draw(rec); 34 randomMovement(painter, randomNum); 35 i = i + 1; 36 } 37 } 38 39 private void randomMovement(SPainter painter, Random randomNum) { 40 int move = randomNum.nextInt(400); 41 painter.moveToCenter(); 42 painter.mfd(move); 43 move = randomNum.nextInt(400); 44 painter.mbk(move); 45 move = randomNum.nextInt(400); 46 painter.mrt(move); 47 move = randomNum.nextInt(400); 48 painter.mlt(move); 49 } 50 51 private static Color randomColor() { 52 Random rgen = new Random(); 53 int r = rgen.nextInt(256); 54 int g = rgen.nextInt(256); 55 int b = rgen.nextInt(256); 56 return new Color(r,g,b); 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 } 71