Fundamentals of Computer Science II (CSC-152 2000S)


Class 32: Lab: Animation

Back to Implementing Lists. On to Discussion of Exam 2.

Held Friday, March 17, 2000

Overview

Today we use the down time before break to play with animation in Java.

Notes

Contents

Summary


A Simple Animation Applet

import java.awt.Color;     // Yes, we care about colors
import java.awt.Frame;     // The window that holds our baloon
import java.awt.Graphics;  // Our handy-dandy paintbrush
import java.awt.Rectangle; // For the bounds

/**
 * A simple application that animates an expanding circle.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of March 2000
 */
public class Balloon
  extends Frame
  implements Runnable
{

  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+

  /**
   * The color of the balloon.
   */
  protected Color color;

  /**
   * The radius of the balloon.
   */
  protected int radius;

  /**
   * The x coordinate of the center of the balloon.
   */
  protected int xcoord;

  /**
   * The y coordinate of the center of the balloon.
   */
  protected int ycoord;

  /**
   * The amount to change the radius by.
   */
  protected int deltaRadius;

  /**
   * The thread of computation associated with this object.
   */
  protected Thread thread;

  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+

  public Balloon(Color initialColor) {
    this.color = initialColor;
    this.radius = 10;
    this.deltaRadius = 5;
    this.xcoord = 50;
    this.ycoord = 300;
    this.setSize(400,400);
    this.show();
    this.thread = new Thread(this);
    this.thread.setPriority(Thread.MIN_PRIORITY);
    this.thread.start();
  } // Balloon

  // +-----------+-----------------------------------------------
  // | Modifiers |
  // +-----------+

  /**
   * Update the balloon.
   */
  public void update() {
    this.radius = this.radius + this.deltaRadius;
    this.xcoord = this.xcoord + 1;
    this.ycoord = this.ycoord - 2;
  } // update()

  // +-------------------------+---------------------------------
  // | Overriden Frame Methods |
  // +-------------------------+

  /**
   * Paint the current version of the balloon.
   */
  public void paint(Graphics paintBrush) {
    // System.err.println("Painting ...");
    Rectangle bounds = this.getBounds();
    if (this.radius < 10) {
      this.deltaRadius = 5;
    }
    else if (2*this.radius > bounds.width) {
      this.deltaRadius = -5;
    }
    // Paint the balloon
    paintBrush.setColor(this.color);
    paintBrush.fillOval(this.xcoord-this.radius, 
                        this.ycoord-this.radius,
                        this.radius*2,
                        this.radius*2);
  } // paint(Graphics)

  // +------------------+----------------------------------------
  // | Runnable Methods |
  // +------------------+
  
  public void run() {
    while (true) {
      try { this.thread.sleep(200); }
      catch (InterruptedException e) { }
      update();
      repaint();
    } // while
  } // run()

  // +------+----------------------------------------------------
  // | Main |
  // +------+

  public static void main(String[] args) {
    new Balloon(Color.red);
  }
} // class Balloon

Painting

Threads of Computation

Lab


History

Tuesday, 18 January 2000

Wednesday, 17 March 2000

Back to Implementing Lists. On to Discussion of Exam 2.


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/2000S/Outlines/outline.32.html

Source text last modified Fri Mar 17 09:49:48 2000.

This page generated on Fri Mar 17 09:55:02 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu