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 12 public Invention2() { 13 paintTheImage(); 14 } 15 16 private void paintTheImage() { 17 SPainter painter = new SPainter("Random Rectangles", 800, 800); 18 int nrOfRectangles = 300; 19 paintRectangles(painter, nrOfRectangles); 20 } 21 22 private void paintOneRectangle(SPainter painter) { 23 Random colors = new Random(); 24 int random = colors.nextInt(6); 25 if (random == 0) { 26 painter.setColor(Color.BLACK); 27 } 28 else if(random ==1) { 29 painter.setColor(Color.RED); 30 } 31 else if(random ==2) { 32 painter.setColor(Color.GREEN); 33 } 34 else if(random ==3) { 35 painter.setColor(Color.BLUE); 36 } 37 else if(random ==4) { 38 painter.setColor(Color.YELLOW); 39 } 40 else{ 41 painter.setColor(Color.CYAN); 42 } 43 44 double Height = number(25,100); 45 double Width = number(25,100); 46 47 painter.move(); 48 SRectangle Rectangles= new SRectangle(Height, Width); 49 painter.paint(Rectangles); 50 painter.setColor(Color.BLACK); 51 painter.draw(Rectangles); 52 } 53 54 private static double number(int min, int max) { 55 double randomInteger = (int)(Math.random()*((max-min))+min); 56 return randomInteger; 57 } 58 59 private void paintRectangles(SPainter painter, int nrOfRectangles) { 60 int i =1; 61 while ( i <= nrOfRectangles) { 62 paintOneRectangle(painter); 63 i++; 64 } 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 }