LAB 14  (Comp 170) - Simple Animation

In this lab, we continue to explore AWT:  this time to create applications involving simple animation.  Animation, of course, is the illusion of motion.  We employ simple loops and drawing methods from the Graphics class of AWT to achieve this goal.

I   Our first example offers an illustration of a balloon that is being inflated and finally bursts.

import java.awt.*;
import java.awt.event.*;

public class Balloon extends Frame implements ActionListener{

    public static void main(String[ ] args) {
       Frame f = new Balloon();
       f.resize(400, 400);
       f.show();
    }
    private static boolean startClicked = false;

    public void actionPerformed(ActionEvent e){
        String c = e.getActionCommand();
        if (c.equals("start")){
          startClicked = true;
          repaint();
        }
    }
    public void paint(Graphics g) {
        Dimension size = getSize();
        g.setColor(Color.magenta);
        g.fillRect(0, 0, 400, 400);
        g.setColor(Color.magenta);
        g.setColor(Color.black);
        g.setFont(new Font ("Helvetica", Font.BOLD, 15));
        if (!startClicked){
            g.drawString("Watch my balloon expand!", 95,100);
        }
        else{
            int radius=0;
            int xCoord=size.width/2;
            int yCoord=size.height/2;
            int minCoord = (int)Math.min(xCoord,yCoord);
            final int PAUSE = 1000000;   // Question A
            while (2*radius<minCoord){ // Question B
                g.setColor(Color.yellow);
                g.fillOval(xCoord-radius,yCoord-radius,2*radius,2*radius);
                radius++;
                for(int i=0; i<PAUSE;i++);  // time delay
            }
            g.setColor(Color.magenta);
            g.fillRect(0, 0, 400, 400);
            g.setColor(Color.black);
            g.setFont(new Font ("Helvetica", Font.BOLD, 75));
            g.drawString("POP!", xCoord/2 ,yCoord+5);  // Question C
        }
    }
    public  Balloon( ) {
        setSize(300, 300);
        setBackground(Color.cyan);
        setTitle("Expanding Balloon");
        setLayout(new BorderLayout());
        Panel ButtonPanel = new Panel();
        Button start = new Button("start");
        start.addActionListener(this);
        ButtonPanel.add(start);
        add(ButtonPanel, "South");
    }
    public boolean handleEvent(Event e) {
        if (e.id==Event.WINDOW_DESTROY)
        System.exit(0);
        return super.handleEvent(e);
    }
}
 

(A)     What is the function of the constant PAUSE?  What happens if PAUSE is halved?  doubled?  tripled?

(B)     What happens if the line  while (2*radius<minCoord) is changed to while (3*radius<minCoord)?  or to while (radius<minCoord) ?

(C)      Add a vertical "cord" (using drawLine() ) to the balloon which begins at the bottom of the balloon, and stops at the bottom of the window.

(D)      Rewrite the code so that the balloon is originally fully inflated, and over time is deflated until nothing remains.  Change the "Pop!" message to "Fully deflated!"  or anything else which you find appropriate.

II     The following program offers the illusion of a rotating disk, having a different color on each side.

import java.awt.*;
import java.awt.event.*;

public class Rotate extends Frame implements ActionListener{

    public static void main(String[ ] args) {
       Frame f = new Rotate ();
       f.resize(400, 400);
       f.show();
    }
    private static boolean startClicked = false;

    public void actionPerformed(ActionEvent e){
        String c = e.getActionCommand();
        if (c.equals("start")){
          startClicked = true;
          repaint();
        }
    }

    public void paint(Graphics g) {
        if (startClicked){
            int width=0, height=150;
            int x=200, y=100, warp=1;
            for(int change=0; change<500; change++){
                width+=warp*2;
                x-=warp;
                if(width==0 || width==150)
                    warp*=-1;  //switch between growing and shrinking
                if ((change/150)%2 == 0){// choose color depending upon
                                    // which side of disc faces us
                    g.setColor(Color.yellow);  // side one
                }
                else{
                     g.setColor(Color.blue);   // side two
                }
                g.fillOval(x,y,width,height);
                for(int pause=1; pause<=1500000;pause++);
                g.setColor(getBackground());
                g.fillOval(x,y,width,height); //erase oval
            }
        }
        else{
            g.setColor(Color.black);
            g.setFont(new Font ("Helvetica", Font.BOLD, 25));
            g.drawString("Watch me rotate!", 75, 80);
        }
    }
    public  Rotate ( ) {
        setSize(300, 300);
        setBackground(Color.magenta);
        setTitle("Rotating Disk");
        setLayout(new BorderLayout());

        Panel ButtonPanel = new Panel();
        ButtonPanel.setBackground(Color.white);
        Button start = new Button("start");
        start.addActionListener(this);
        ButtonPanel.add(start);
        add(ButtonPanel, "South");
    }
    public boolean handleEvent(Event e) {
        if (e.id==Event.WINDOW_DESTROY)
             System.exit(0);
        return super.handleEvent(e);
    }
}

(A)   Change the speed of rotation of the disc.

(B)    Change the code so that only one complete rotation is made for each click of the Start button.

(C)    Modify the code so that a rectangle is being rotated about a horizontal (rather than a vertical) axis.
 

III     Our final program produces the illusion of a ball traveling through space.  The ball accelerates as it grows closer to "ground level."  (Contrary to Newtonian mechanics, this ball is following a linear trajectory --- to make the code easier.)
 

import java.awt.*;
import java.awt.event.*;

public class BallFall extends Frame implements ActionListener{
    public static void main(String[ ] args) {
       Frame f = new BallFall();
       f.resize(400, 400);
       f.show();
    }
    private static boolean startClicked = false;

    public void actionPerformed(ActionEvent e){
        String c = e.getActionCommand();
        if (c.equals("start")){
            startClicked = true;
            repaint();
        }
    }
    public void paint(Graphics g) {
        if (!startClicked){
            g.setColor(Color.black);
            g.setFont(new Font ("Helvetica", Font.BOLD, 15));
            g.drawString("Watch this ball fall!", 100, 100);
        }
        else{
            int width=30, height=30;
            int x=0,y=0;
            Color[] colorArray = {Color.cyan, Color.yellow, Color.red, Color.green};
            final int PAUSE = 50000000;
            for(int change=0; change<500 && x<400; change++){
                 g.setColor(colorArray[x/85]);
                 g.fillOval(x,y,width,height);
                 for(int pause=0; pause<PAUSE/(y+1); pause++);
                 g.setColor(getBackground());//change to background color for erase
                 //erase
                 g.fillOval(x,y,width,height); //erase oval
                 x++;
                 y++;
            }
        }
    }
    public  BallFall( ) {
         setSize(300, 300);
         setBackground(Color.magenta);
         setTitle("Moving Ball");
         setLayout(new BorderLayout());
         Panel ButtonPanel = new Panel();
         ButtonPanel.setBackground(Color.white);
         Button start = new Button("start");
         start.addActionListener(this);
         ButtonPanel.add(start);
         add(ButtonPanel, "South");
    }
    public boolean handleEvent(Event e) {
        if (e.id==Event.WINDOW_DESTROY)
            System.exit(0);
        return super.handleEvent(e);
    }
}

(A)   Run the Java code for the fallling ball.  How would you change the speed of the ball?

(B)  Change the direction of motion, so that the ball is rising rather than falling.
 
 

Here we go round the prickly pear
Prickly pear prickly pear
Here we go round the prickly pear
At five o'clock in the morning.

Between the idea
And the reality
Between the motion
And the act
Falls the Shadow.

   - T. S. Eliot, The Hollow Men


Course Home Page          Department Home Page        Loyola Home Page