Your task is to draw n balloons centered at random positions, each with a randomly colored circle and a white string.

The BalloonComponent class generates n balloons centered at random positions in the frame and draws them with a randomly colored circle and a white string. The Balloon class stores an Ellipse2D.Double object that defines the circle and a Line2D.Double object that defines the string.

Here is a sample program output:

.

Complete the following file:

BalloonComponent.java

import javax.swing.JComponent; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Color; import java.awt.Rectangle; import java.util.Random; /** This class displays a number of balloons. */ public class BalloonComponent extends JComponent { private int balloonCount; /** Constructor for a BalloonComponent with a number of balloons @param n the number of balloons to show */ public BalloonComponent(int n) { balloonCount = n; } public void paintComponent(Graphics g) { final Color SKY_BLUE = new Color(165,218,239); final int MAX_RADIUS = 30; Graphics2D g2 = (Graphics2D) g; // Draw the sky Rectangle sky = new Rectangle (0, 0 , getWidth(), getHeight()); g2.setColor(SKY_BLUE); g2.fill(sky); // Use this random number generator Random generator = new Random(42); // Generate and draw balloons // Your work here } }

Use the following files:

Balloon.java

import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.Color;
import java.awt.Graphics2D;

/**
   This class draws a balloon.
*/
public class Balloon
{
   private Ellipse2D.Double balloon;
   private Line2D.Double string;
   private Color color;

   /**
      Construct a balloon.   
      @param centerx the x-coordinate of the balloon's center
      @param centery the y-coordinate of the balloon's center
      @param radius the balloon's radius
      @param color the balloon's color
   */
   public Balloon(int centerx, int centery, int radius, Color color)
   {
      balloon = new Ellipse2D.Double(centerx - radius, centery - radius,
         2 * radius, 2 * radius);
                      
      string = new Line2D.Double(centerx, centery + radius, centerx, centery + 3 * radius);
      this.color = color;
   }

   public void draw(Graphics2D g2)
   {
      g2.setColor(color);
      g2.fill(balloon);
      g2.setColor(Color.WHITE);
      g2.draw(string);
   }
}

BalloonViewer.java

import javax.swing.*;

/**
   This class shows a frame containing random balloons.
*/
public class BalloonViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      final int NBALLOONS = 40;
      
      frame.setSize(400, 400);
      frame.setTitle("BalloonViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      BalloonComponent component = new BalloonComponent(NBALLOONS);
      frame.add(component);
      
      frame.setVisible(true);
   }
}