1 /* 2 * program to attempt to make a picture that is different everytime 3 * this will resemble a russian nesting dolls 4 */ 5 6 package npw; 7 8 import painter.SPainter; 9 import shapes.SRectangle; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Random; 14 15 16 public class Invention2 { 17 public void paintTheImage() { 18 //establish painter and rectangle 19 Random rgen = new Random(); 20 //get a bunch of random numbers 21 int rectangleWidth = 50*(5+rgen.nextInt(6)); 22 int rectangleHeight = 50*(5+rgen.nextInt(16)); 23 int nrOfDolls = 2+rgen.nextInt(10); 24 //create some objects 25 SPainter vlad = new SPainter("Invention2", 1400, 900); 26 SRectangle rectangle = new SRectangle(rectangleHeight,rectangleWidth); 27 vlad.setColor(randomColor(rgen)); 28 getPainting(rgen,rectangle,vlad,nrOfDolls); 29 printResults(nrOfDolls,rectangleHeight,rectangleWidth); 30 } 31 32 private void printResults(int nrOfDolls,int rectangleHeight, int rectangleWidth) { 33 System.out.println("nrOfDolls = " + nrOfDolls); 34 System.out.println("Rectangle height = " + rectangleHeight); 35 System.out.println("Rectangle width = " + rectangleWidth); 36 } 37 38 private int getPainting(Random rgen, SRectangle rectangle, SPainter vlad, int nrOfDolls) { 39 40 boolean bool = rgen.nextBoolean(); 41 42 //position painter left or right 43 if (bool) { 44 vlad.mlt(0.5*vlad.canvasWidth()); 45 } 46 else { 47 vlad.mrt(0.5*vlad.canvasWidth()); 48 } 49 //paint rectangle 50 int i = 1; 51 while (i <= nrOfDolls) { 52 vlad.paint(rectangle); 53 if(bool) { 54 vlad.mrt(rectangle.width()*1.1); 55 } 56 else { 57 vlad.mlt(rectangle.width()*1.1); 58 } 59 rectangle.shrink((rectangle.height()/nrOfDolls),(rectangle.width()/nrOfDolls)); 60 i = i+1; 61 } 62 return nrOfDolls; 63 64 } 65 66 private Color randomColor(Random rgen) { 67 int r = rgen.nextInt(256); 68 int g = rgen.nextInt(256); 69 int b = rgen.nextInt(256); 70 if (r+g+b == 765) { 71 return Color.black; 72 } 73 else { 74 return new Color(r, b, g); 75 } 76 } 77 78 public Invention2() { 79 paintTheImage(); 80 } 81 public static void main(String[] args) { 82 SwingUtilities.invokeLater(new Runnable() { 83 @Override 84 public void run() { 85 new Invention2(); 86 } 87 }); 88 } 89 } 90 91