1 /* 2 * Program that paints 300 balloons of 6 different random colors in a blue sky. 3 * It will feature commands. 4 */ 5 6 package npw; 7 8 import java.awt.Color; 9 import java.util.Random; 10 import javax.swing.SwingUtilities; 11 import painter.SPainter; 12 import shapes.SCircle; 13 import shapes.SSquare; 14 15 public class AlternateBalloons { 16 17 //Random Color 18 Color c1 = randomColor(); 19 Color c2 = randomColor(); 20 Color c3 = randomColor(); 21 Color c4 = randomColor(); 22 Color c5 = randomColor(); 23 Color c6 = randomColor(); 24 25 //REQUIRED INFRASTRUCTURE 26 27 public AlternateBalloons() { 28 paintTheImage(); 29 } 30 31 public static void main(String[] args) { 32 SwingUtilities.invokeLater(new Runnable() { 33 public void run() { 34 new AlternateBalloons(); 35 } 36 37 }); 38 } 39 40 //THE PAINTER DOING ITS THING 41 42 private void paintTheImage() { 43 SPainter painter = new SPainter("Alternate Balloons", 600, 600); 44 paintSky(painter); 45 int nrOfBalloons = 300; 46 paintBalloons(painter,nrOfBalloons); 47 } 48 49 private void paintSky(SPainter painter) { 50 painter.setColor(Color.BLUE); 51 SSquare sky = new SSquare(2000); 52 painter.paint(sky); 53 } 54 55 private void paintBalloons(SPainter painter, int nrOfBalloons) { 56 int i = 1; 57 58 while (i <= nrOfBalloons) { 59 paintOneBalloon(painter); //ask IntelliJ to generate the stub 60 i = i + 1; 61 } 62 } 63 64 private static Color randomColor(){ 65 int rv = (int)(Math.random()*256); 66 int gv = (int)(Math.random()*256); 67 int bv = (int)(Math.random()*256); 68 return new Color(rv,gv,bv); 69 } 70 71 private void paintOneBalloon(SPainter painter) { 72 Random rgen = new Random(); 73 int rn = rgen.nextInt(6); 74 75 if (rn == 0) { 76 painter.setColor(c1); 77 } else if (rn == 1) { 78 painter.setColor(c2); 79 } else if (rn == 2) { 80 painter.setColor(c3); 81 } else if (rn == 3) { 82 painter.setColor(c4); 83 } else if (rn == 4) { 84 painter.setColor(c5); 85 }else { 86 painter.setColor(c6); 87 } 88 painter.move(); 89 int balloonRadius = 30; 90 SCircle balloon = new SCircle(balloonRadius); 91 painter.paint(balloon); 92 painter.setColor(Color.BLACK); 93 painter.draw(balloon); 94 95 } 96 97 98 99 } 100