1 /* 2 This program paints an image subject for constraints. 3 */ 4 package npw; 5 import java.awt.*; 6 import javax.swing.SwingUtilities; 7 import java.util.Random; 8 import painter.SPainter; 9 import shapes.SRectangle; 10 11 public class Invention2 { 12 private void paintTheImage() { 13 SPainter painter = new SPainter("Invention 2", 700, 700); 14 Random randomNum = new Random(); 15 painter.setBrushWidth(3); 16 int i = 1; 17 while (i <= 100) { 18 int recHeight = randomNum.nextInt(200); 19 int recWidth = randomNum.nextInt(200); 20 if (recHeight < 50) { 21 recHeight = 50; 22 } 23 if (recWidth < 50) { 24 recWidth = 50; 25 } 26 SRectangle rec = new SRectangle(recHeight, recWidth); 27 painter.setColor(randomColor()); 28 painter.paint(rec); 29 painter.setColor(Color.BLACK); 30 painter.draw(rec); 31 randomMovement(painter, randomNum); 32 i = i + 1; 33 } 34 } 35 36 private void randomMovement(SPainter painter, Random randomNum) { 37 int move = randomNum.nextInt(400); 38 painter.moveToCenter(); 39 painter.mfd(move); 40 move = randomNum.nextInt(400); 41 painter.mbk(move); 42 move = randomNum.nextInt(400); 43 painter.mrt(move); 44 move = randomNum.nextInt(400); 45 painter.mlt(move); 46 } 47 48 private static Color randomColor() { 49 Random rgen = new Random(); 50 int r = rgen.nextInt(256); 51 int g = rgen.nextInt(256); 52 int b = rgen.nextInt(256); 53 return new Color(r,g,b); 54 } 55 56 public Invention2() { 57 paintTheImage(); 58 } 59 60 public static void main(String[] args) { 61 SwingUtilities.invokeLater(new Runnable() { 62 public void run() { 63 new Invention2(); 64 } 65 }); 66 } 67 } 68 69 70