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 import java.util.Scanner; 10 11 public class Invention2 { 12 13 14 15 16 17 private void paintTheImage(){ 18 19 20 // Establish the painter 21 SPainter painter = new SPainter("Stella", 800, 800); 22 painter.setBrushWidth(3); 23 24 25 int squares = (int)(Math.random() * ((50 - 1) + 1)) + 1; 26 // Paint the squares 27 paintsquares(painter, squares); 28 } 29 30 private void paintsquares(SPainter painter, int squares) { 31 32 double Rpainted = 1; 33 while (Rpainted <= squares) { 34 int sides = (int)(Math.random() * ((700 - 1) + 1)) + 1; 35 SRectangle square = new SRectangle(sides, sides ); 36 37 if (sides < 100) { 38 painter.setColor(Color.black); 39 painter.paint(square); 40 41 } 42 else if (sides >= 100) { 43 painter.setColor(randomColor()); 44 painter.move(); 45 painter.paint(square); 46 } 47 48 49 Rpainted = Rpainted + 1; 50 51 } 52 } 53 54 55 56 57 58 59 60 61 private static Color randomColor() { 62 Random rgen = new Random(); 63 int r = rgen.nextInt(256); 64 int g = rgen.nextInt(256); 65 int b = rgen.nextInt(256); 66 return new Color(r,g,b); 67 } 68 69 public Invention2() { 70 paintTheImage(); 71 } 72 73 public static void main(String[] args) { 74 SwingUtilities.invokeLater(new Runnable() { 75 public void run() { 76 new Invention2(); 77 } 78 }); 79 } 80 81 82 83 84 85 86 87 88 89 } 90