CSC 1012

Introduction to Computer Science

UnpleasantSurprise Applet

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

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

    public void paint (Graphics g)
    {
        while (true)
        {
            animate (g);
        }

    }
       
    public void animate (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);
    }
}


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

Back to CSC 1012 Syllabus