CSC 012

Introduction to Computer Science

Surprise Applet

//File:    Surprise.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Surprise extends Applet
{
    // Surprise with an exploding balloon
    // Author: Rachel McDermott, September 27, 1996

    public void paint (Graphics g)
    {
        Dimension size = getSize();
       
        int     diameter = 0,
        w = size.width,
        h = size.height,
        cmin = (int)Math.min(w, h);

        g.setColor(Color.yellow);
        while (diameter < cmin)
        {
            g.fillOval((w-diameter)/2, (h-diameter)/2,
                diameter, diameter);
            diameter++;
           
            try
            {
                Thread.sleep(10);     // sleep for 10 msec
            }
            catch (InterruptedException t)
            {
            }

        }

        Font current = getFont();
        FontMetrics metrics = getFontMetrics (current);
        int ws = metrics.stringWidth("Surprise!");
        int hf = metrics.getHeight();
       
        g.setColor(Color.red);
        g.drawString("Surprise!", (w-ws)/2, (h+hf)/2);
    }
}

We still have an expanding balloon that ENDS with the word balloon.   If you'd like the balloon to repeat its cycle indefinitely, then we need to introduce an infinite loop.  This is accomplished by reducing the paint() method to an infinite loop that repeats another method which we call animate().

 


Adapted from Bill Steinmetz's course summary by H. J. Bernstein, 8 November 2001, revised 23 October 2003

Back to CSC 1012 Syllabus