1 /* 2 * Program that paints 100 red, yellow, blue, green, magenta, orange balloons in a blue sky. 3 * It will feature commands. 4 */ 5 package npw; 6 7 import painter.SPainter; 8 import shapes.SCircle; 9 import shapes.SSquare; 10 11 import javax.swing.*; 12 import java.awt.*; 13 import java.util.Random; 14 15 public class AlternateBalloons { 16 17 public static void main(String[] args) { 18 SwingUtilities.invokeLater(new Runnable() { 19 public void run() { 20 new AlternateBalloons(); 21 } 22 }); 23 } 24 // THE PAINTER DOING ITS THING 25 private void paintTheImage() { 26 SPainter painter = new SPainter("Balloons", 600, 600); 27 paintSky(painter); 28 int nrOfBalloons = 300; 29 paintBalloons(painter,nrOfBalloons); 30 } //PAINT THE SKY 31 private void paintSky(SPainter painter) { 32 painter.setColor(Color.BLUE); 33 SSquare sky = new SSquare(2000); 34 painter.paint(sky); 35 } 36 private void paintBalloons(SPainter painter, int nrOfBalloons) { 37 int i = 1; 38 while ( i <= nrOfBalloons ) { //IF I IS LESS THAN OR EQUAL TO nrOfBalloons LOOP THIS vvv 39 paintOneBalloon(painter); //GO TO NEW STUB MADE FOR THIS LINE 40 i = i + 1; 41 } 42 }//PAINT THE BALLOONS 43 private void paintOneBalloon(SPainter painter) { 44 Random rgen = new Random(); 45 int rn = rgen.nextInt(6); 46 if ( rn == 0 ) { 47 painter.setColor(Color.RED); 48 } else if ( rn == 1 ) { 49 painter.setColor(Color.ORANGE); 50 } else if ( rn == 2 ) { 51 painter.setColor(Color.BLUE); 52 } else if ( rn == 3 ) { 53 painter.setColor(Color.GREEN); 54 } else if ( rn == 4) { 55 painter.setColor(Color.MAGENTA); 56 } else { 57 painter.setColor(Color.YELLOW); 58 } 59 painter.move(); 60 int balloonRadius = 30; 61 SCircle balloon = new SCircle(balloonRadius); 62 painter.paint(balloon); 63 painter.setColor(Color.BLACK); 64 painter.draw(balloon); 65 } 66 // REQUIRED INFRASTRUCTURE 67 public AlternateBalloons() { paintTheImage(); 68 } 69 } 70