Balloon Drop

We have two starter classes: Balloon and BalloonFall. The application will add a new Balloon to an ArrayList<Balloon> called balloons when the user types a key, and the balloons will fall when the timer goes off by making an ActionEvent.

See if you can make a new class called BalloonRise that make Balloon objects that rise rather than fall.

Watch the tutorial


  1. Add a method in the Balloon class called rise() which is the reverse of drop()
  2. Make a copy of BalloonFall to use as a starter for a new class called BalloonRise
  3. Change the name in paintComponent() to "Balloon Rise"
  4. Change keyReleased so the new Balloon is near HEIGHT rather than 0.
  5. Change actionPerformed so the balloon rises rather than falls
  6. Change actionPerformed so that it removes the balloon if balloons.get(i).getBottom() < 0
  7. Modify the paintComponent() method to show the number of balloons: g.drawString(balloons.size()+" Balloons", WIDTH-100, 20);


  1. Can you make the letter that was pressed to make the balloon drawn at the center of the balloon? (Hint: in the keyReleased method e.getKeyChar() returns the char typed
  2. Can you make an application that has more Balloons drop (or rise) randomly and typing the keys "pops" (or removes) them?
  3. Instead of Balloon objects, have letters drop and have them removed when you type the letter? You can keep score and make it into a game.

import java.awt.Color;
import java.awt.Graphics;

public class Balloon {
	private Color color;
	private int x,y,width,height, deltaY;

	public Balloon(int x, int y, int size) {
		this.deltaY=randomInt(1, 5);
		int red=randomInt(0, 255);
		int green=randomInt(0, 255);
		int blue=randomInt(0, 255);
		this.color = new Color(red, green,blue);
	public void draw(Graphics g){
		g.fillOval(x, y, width, height);
	public void drop(){
	public int getBottom() {
		return y+height;
	public static int randomInt(int min, int max){
		return min+(int)((max-min+1)*Math.random());


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class BalloonFall extends JPanel implements KeyListener, ActionListener
	public static int WIDTH=800;
	public static int HEIGHT=600;
    private Font titleFont, regularFont;
    private ArrayList<Balloon> balloons;
    private Timer timer;
	 * The class constructor will initialize the ArrayList<Balloon>
	 * and other object fields (variables), including a Timer
	 * object from the javax.swing.* library.  The timer will
	 * generate an Action Event, so that something happens
	 * in regular intervals without the user typing anything.
    public BalloonFall()
    	//initialize variables here...
    	titleFont = new Font("Roman", Font.BOLD, 18);
    	regularFont = new Font("Helvetica", Font.PLAIN, 12);
    	balloons = new ArrayList<Balloon>();
    	timer = new Timer(10, this);
	 * the main method makes an instance of our application and puts it in a JFrame 
	 * that will end the application when it is closed.
	public static void main(String[] args) {
		BalloonFall app= new BalloonFall();
		JFrame window = new JFrame("Balloon Fall");
		window.setSize(WIDTH, HEIGHT);

	 *  This is the method to change what is drawn to the screen:
	public void paintComponent(Graphics g){
		g.fillRect(0, 0, getWidth(),getHeight());
		g.drawString("BalloonFall version 1.0", 20, 20);
		//g.drawString(balloons.size()+" Balloons", WIDTH-100, 20);
		g.drawString("Press a Key to make a Balloon", 20, 40);
		for (Balloon x:balloons){


	 * These 3 methods need to be declared to implement the KeyListener Interface
	 * that listens for keys that are typed on the keyboard
	public void keyTyped(KeyEvent e) {}

	public void keyPressed(KeyEvent e) {}

	public void keyReleased(KeyEvent e) {

		int size = Balloon.randomInt(50, 80);
		int x = Balloon.randomInt(0, WIDTH);
		int y = Balloon.randomInt(-size, 0);
		balloons.add(new Balloon(x,y,size));
	 * This method is needed to implement the ActionListener that listens for the timer
	 * e.getKeyChar() and e.getKeyCode() will return the char (or its code)
	public void actionPerformed(ActionEvent e) {
		// timer made an action event, so
		int i=0;
		while (i<balloons.size()){
			if (balloons.get(i).getBottom()>HEIGHT){