1 /* 2 * Program to paint an image subject to the following constraints: 3 * 1. It uses at least 1 while statement in a non-trivial way 4 * 2. It uses at least 1 if statement in a non-trivial way 5 * 3. It features solely rectangles, all generated from just 1 rectangle 6 * 4. The program takes no input from the user of the program 7 * 5. It creates a different image each time the program is executed, different with respect 8 * to number or size or color or whatever of the featured object 9 * 6. It is kinda interesting to look at 10 */ 11 12 package npw; 13 14 import painter.SPainter; 15 import shapes.SRectangle; 16 import javax.swing.*; 17 import java.awt.*; 18 19 public class Invention2 { 20 21 private void paintTheImage() { 22 SPainter painter = new SPainter("Nondeterministic Invention Stars", 600, 600); 23 int nrOfStars = 500; //500 stars to paint 24 int skyHeight = 800; //800 to make sure painter is covered 25 int skyWidth = 800; //800 to make sure painter is covered 26 SRectangle sky = new SRectangle(skyHeight, skyWidth); 27 paintSky(painter, sky); //paint black sky 28 paintStars(painter, sky, nrOfStars); //paint white stars 29 } 30 31 private void paintSky(SPainter painter, SRectangle sky) { 32 painter.setColor(Color.BLACK); 33 painter.paint(sky); 34 } 35 36 private void paintStars(SPainter painter, SRectangle star, int nrOfStars) { 37 painter.setColor(Color.WHITE); 38 star.shrink(794, 794); //shrink it to itsy bitsy 39 int i = 1; 40 while ( i <= nrOfStars ) { //counter 41 if (i % 2 == 0) { //if even, make stars that are small and yellow 42 painter.setColor(Color.YELLOW); 43 painter.faceNorth(); //set painter heading to 0 degrees 44 painter.move(); //random position 45 painter.paint(star); 46 star.expand(5, 5); 47 painter.paint(star); 48 painter.tl(45); 49 painter.paint(star); 50 star.shrink(5, 5); //make invariant 51 painter.tr(45); //make invariant 52 painter.setColor(Color.WHITE); //make invariant 53 } else if (i % 5 == 0){ //For the stars numbers that are divisible by 5, make a random colored star 54 Color color = randomColor(); //assign random RGB color 55 painter.setColor(color); 56 painter.faceNorth(); //set painter heading to 0 degrees 57 painter.move(); //random position 58 painter.paint(star); 59 painter.tl(45); 60 painter.paint(star); //second rectangle to create star look 61 painter.tr(45); //make invariant 62 } else { //if not even or divisible by 5, make stars that are larger and white 63 painter.faceNorth(); 64 painter.move(); //random position 65 star.expand(10, 10); 66 painter.paint(star); 67 painter.tl(45); //rotate 45 degrees 68 painter.paint(star); 69 painter.tr(45); //make invariant 70 star.shrink(10,10); //make invariant 71 } 72 i++; 73 } 74 } 75 76 private static Color randomColor() { 77 int rv = (int)(Math.random()*256); 78 int gv = (int)(Math.random()*256); 79 int bv = (int)(Math.random()*256); 80 return new Color(rv, gv, bv); 81 } 82 83 // REQUIRED INFRASTRUCTURE 84 public Invention2() { 85 paintTheImage(); 86 } 87 88 public static void main(String[] args) { 89 SwingUtilities.invokeLater(new Runnable() { 90 public void run() { 91 new npw.Invention2(); 92 } 93 }); 94 } 95 }