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