CSC 1012

Introduction to Computer Science

Surprise2 Applet

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

public class Surprise2 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);
       
        try
        {
            Thread.sleep(1000); // sleep for 1 sec
        }
        catch (InterruptedException t)
        {
        }
       
        repaint();    //calls update() which CLEARS THE WINDOW,
                //then calls paint()

    }
}

hr>

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

Back to CSC 1012 Syllabus