email me at borlaj@portlandschools.org
notes previous (09/07/06) submit the dump links  
 

Timing

 

To do animation or anything that requires delay, you need to implement the class runnable. The code is a little complex; it uses something called threads to delay operating. You dont need to understand the code. All you need to do is implement runnable in your code decleration, and include the code below the **********************.

 

  • The only editable section of that code is the int delay which is in millliseconds.
  • When the applet is run, the delay is started automatically and update is called every 50 milliseconds (or whatever the delay is).
  • To stop the animation, call the method stop()

 

 
 
// This class is to test animation
// Designed by Jeff Borland
// Date 12:10:10
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TestAnimation extends JApplet implements  MouseListener, MouseMotionListener,ActionListener,Runnable,KeyListener
{
    int x=0;
    
    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
        addKeyListener(this);
        setFocusable(true);

        Container screen = getContentPane();
        screen.setBackground(Color.white);
        screen.setLayout (new FlowLayout() );

        
    }

    public void paint (Graphics g)
    {
        super.paint(g); 
    }

    //update gets called automatically for each frame of the animation
    //this will then get called ever 50 milliseconds (the delay [below])
    public void update()
    {
        Graphics g=getGraphics();
        g.clearRect(0,0,500,500);
        g.drawString("Mr. Boolean",x,50);
        x++;
    }   

    //This is the method that will be called if a key is pressed
    //It is neccessary if you are using getKeyCode()
    public void keyPressed(KeyEvent e)
    {
        char theChar=e.getKeyChar();
        int theCode=e.getKeyCode();

        
    }

    //This is the method that will be called if a key is released
    //It is unneccessary to use this method, but you must include it
    public void keyReleased(KeyEvent e)
    {   
    }

    //This is the method that will be called if a key is typed, however you cant use getKeyCode, so
    // you are better to use keyPressed
    public void keyTyped(KeyEvent e)
    {     
    }

    //when someone presses the mouse button
    public void mousePressed(MouseEvent e)
    {      

    }

    //when someone releases the mouse button
    public void mouseReleased(MouseEvent e)
    {      

    }

    // when the mouse enters the applet
    public void mouseEntered(MouseEvent e)
    {      

    }

    //when the mouse leaves the applet
    public void mouseExited(MouseEvent e)
    {      

    }

    //when the mouse button is clicked
    public void mouseClicked(MouseEvent e)
    {
       requestFocus(); //this bring the focus make to the main progra
       
    }

    //the mouse button is pressed and the mouse makes a significantly large movement
    public void mouseDragged(MouseEvent e)
    {

    }

    //the mouse makes a significantly large movement
    public void mouseMoved(MouseEvent e)
    {
    }

    public void actionPerformed(ActionEvent thisEvent)
    {
        Object source = thisEvent.getSource();

        //now have if statements seeing finding out where the action occured

    }
    
    
    /*********************************************************************************************/
    /* BELOW IS FOR ANIMATION.  THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY */

    int frame;
    int delay=50;   // this is the time of the delay in milliseconds.
    Thread animator;

    /**
     * This method is called when the applet becomes visible on
     * the screen. Create a thread and start it.
     */
    public void start()
    {
        animator = new Thread(this);
        animator.start();
    }

    /**
     * This method is called by the thread that was created in
     * the start method. It does the main animation.
     */
    public void run()
    {
        // Remember the starting time
        long tm = System.currentTimeMillis();
        while (Thread.currentThread() == animator)
        {
            // Display the next frame of animation.
            update();
            try
            {
                tm += delay;
                Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
            }
            catch (InterruptedException e)
            {
                break;
            }
            // Advance the frame
            frame++;
        }
    }

    /**
     * This method is called when the applet is no longer
     * visible. Set the animator variable to null so that the
     * thread will exit before displaying the next frame.
     */
    public void stop()
    {
        animator = null;
    }
}

 

Assignment:

  1. Add code so that every 40 times it moves the color changes between green and red.
  2. Add code so that when they press 1-3 the animation changes speed. (change the delay - which already is a global variable)
  3. Add a button that stops the animation and a button to start the animation.
  4. Add an oval (or a heart) that gets bigger and smaller just like our heart beats for VLAD.