1 /* 2 * Program that paints 300 different colored balloons in six "nameless" 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 // 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("Balloons",600,600); 35 paintSky(painter); 36 int nrOfBalloons = 300; 37 paintAlternateBalloons(painter,nrOfBalloons); 38 } 39 40 private void paintSky(SPainter painter) { 41 painter.setColor(Color.BLUE); 42 SSquare sky = new SSquare(2000); 43 painter.paint(sky); 44 } 45 46 private void paintAlternateBalloons(SPainter painter, int nrOfBalloons) { 47 int i = 1; 48 while ( i <= nrOfBalloons ) { 49 paintOneBalloon(painter); 50 i = i + 1; 51 } 52 } 53 private void paintOneBalloon(SPainter painter) { 54 Random rgen = new Random(); 55 int rn = rgen.nextInt(6); 56 Color one = new Color(0x0000af); 57 Color two = new Color(0x00af00); 58 Color three = new Color(0xaf0000); 59 Color four = new Color(0x00afaf); 60 Color five = new Color(0xaf00af); 61 Color six = new Color(0xafaf00); 62 if ( rn == 0) { 63 painter.setColor(one); 64 } else if ( rn == 1 ) { 65 painter.setColor(two); 66 } else if ( rn == 2 ) { 67 painter.setColor(three); 68 } else if ( rn == 3 ) { 69 painter.setColor(four); 70 } else if ( rn == 4 ) { 71 painter.setColor(five); 72 } else { 73 painter.setColor(six); 74 } 75 painter.move(); 76 int balloonRadius = 30; 77 SCircle balloon = new SCircle(balloonRadius); 78 painter.paint(balloon); 79 painter.setColor(Color.BLACK); 80 painter.draw(balloon); 81 } 82 }