1 /* 2 * Program that paints 300 balloons in a blue sky. 3 * 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 // REQUIRED INFRASTRUCTURE 18 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 33 private void paintTheImage() { 34 SPainter painter = new SPainter("AlternateBalloons",600,600); 35 paintSky(painter); // ask IntelliJ to generate the stub 36 int nrOfBalloons = 300; 37 paintBalloons(painter,nrOfBalloons); // ask IntelliJ to generate the stub 38 } 39 40 private void paintBalloons(SPainter painter, int nrOfBalloons) { 41 int i = 1; 42 while ( i <= nrOfBalloons ) { 43 paintOneBalloon(painter); // ask IntelliJ to generate the stub 44 i = i + 1; 45 } 46 } 47 48 private void paintOneBalloon(SPainter painter) { 49 Random rgen = new Random(); 50 int rn = rgen.nextInt(6); 51 Color myColor = new Color(1,45,6 ); 52 Color myColor2 = new Color(125,40,50 ); 53 Color myColor3 = new Color(50,250,100 ); 54 Color myColor4 = new Color(125,45,150 ); 55 Color myColor5 = new Color(245,45,6 ); 56 Color myColor6 = new Color(250,250,100 ); 57 if ( rn == 0 ) { 58 painter.setColor(myColor); 59 } else if (rn == 1 ) { 60 painter.setColor(myColor2); 61 } else if (rn == 2 ) { 62 painter.setColor(myColor3); 63 } else if (rn == 3 ) { 64 painter.setColor(myColor4); 65 } else if (rn == 4 ) { 66 painter.setColor(myColor5); 67 } else { 68 painter.setColor(myColor6); 69 } 70 painter.move(); 71 int balloonRadius = 30; 72 SCircle balloon = new SCircle(balloonRadius); 73 painter.paint(balloon); 74 painter.setColor(Color.BLACK); 75 painter.draw(balloon); 76 } 77 78 private void paintSky(SPainter painter) { 79 painter.setColor(Color.BLUE); 80 SSquare sky = new SSquare(2000); 81 painter.paint(sky); 82 } 83 } 84 85