MathBits.com
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use
 

Animation Demo
 


We are going to amend our "repetitive red circle" program so that each circle is drawn (and then erased) one at a time, giving the illusion that the circle is coming toward the viewer.  (If you remove the "erase" option, you will get a slightly different illusion.)


When we are finished, the last scene will be the circle with "Java Moves!!" printed inside.

 

CODE:                      

//animated circles
import java.awt.*;
import BreezyGUI.*;

public class animatedcircle extends GBFrame
{
    public void paint(Graphics g)
    {
       int x = 50, y = 50, width = 50, height = 50; // we are using a circle
       int i;

       for (i = 1; i <= 10; i++)
       {
            // draw the circle in red
            g.setColor(Color.red);
             g.drawOval(x, y, height, width);
             pause(200);
            // draw the circle in white to erase
            if (i == 10)
                     g.setColor(Color.red); // leaves a red circle when done
            else
                     g.setColor(Color.white);
            g.drawOval(x, y, height, width);
            //make adjustments to move the circle
            width = (int) (width*1.25);
             height = (int) (height*1.25);
      }
      Font ArialB16 = new Font("Arial", Font.BOLD, 16);
      g.setColor(Color.red);
      g.setFont(ArialB16);
      g.drawString("Java Moves!!!", 180, 240);
   }
   //Main
   public static void main (String[] args)
   {
      Frame frm = new animatedcircle();
      frm.setSize (440,440);
      frm.setVisible(true);
   }
   //Method to pause
   public static void pause (int time)
   {
      try
      {
            Thread.sleep(time);
      }
      catch(InterruptedException e)
      {
      }
   }
}
 

 


Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use