Discussion of Activity 7.5 - Experimenting with animation

Sample solution

Code for class FlyingLogo:

import java.awt.*;
import javax.swing.*;
public class FlyingLogo extends JFrame
{
   protected static final int FRAME_WIDTH = 400;
   protected static final int FRAME_HEIGHT = 400;
   protected static int delayTime = 5;
   Logo logo;
   StarrySky starrySky =
      new StarrySky(200,FRAME_WIDTH,FRAME_HEIGHT);

   public FlyingLogo(String title)
   {
      super(title);
      setSize(FRAME_WIDTH,FRAME_HEIGHT);
      setLocation(100,100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // Add the logo...
      logo = new Logo();
      logo.goToPosition(100,100);
      // ...and the starry background.
      Container cp = getContentPane();
      cp.add(starrySky);
   }

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

   public void squareMove()
   {
      // First leg.
      for (int i = 0; i < 200; i++)
      {
         logo.right();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
      // Second leg.
      for (int i = 0; i < 200; i++)
      {
         logo.down();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
      // Third leg.
      for (int i = 0; i < 200; i++)
      {
         logo.left();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
      // Fourth and final leg.
      for (int i = 0; i < 200; i++)
      {
         logo.up();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
   }

   public void downRight()
   {
      for (int i = 0; i < 50; i++)
      {
         logo.down();
         logo.right();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
   }
   public void downLeft()
   {
      for (int i = 0; i < 50; i++)
      {
         logo.down();
         logo.left();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
   }

   public void upRight()
   {
      for (int i = 0; i < 50; i++)
      {
         logo.up();
         logo.right();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
   }

   public void upLeft()
   {
      for (int i = 0; i < 50; i++)
      {
         logo.up();
         logo.left();
         repaint();
         try
         {
            Thread.sleep(delayTime);
         }
         catch(Exception e)
         {
            System.exit(0);
         }
      }
   }

   public void figureOfEight()
   {
      downRight();
      downLeft();
      downLeft();
      downRight();
      upRight();
      upLeft();
      upLeft();
      upRight();
   }
}

Code for class FlyingLogoTest for the first task:

public class FlyingLogoTest
{
   public static void main (String[] args)
   {
      FlyingLogo world = new FlyingLogo("Open in space");
      world.setVisible(true);

      world.squareMove();
   }
}

Code for class FlyingLogoTest for the second task will replace the method call to world.squareMove() by a call to world.figureOfEight().

Code for the class FlyingLogoTest for the third task:

public class FlyingLogoTest
{
   public static void main (String[] args)
   {
      FlyingLogo world = new FlyingLogo("Open in space");
      world.setVisible(true);

      FlyingLogo world2 = new FlyingLogo("Open in space again");
      world2.setLocation(500,100);
      world2.setVisible(true);

      world.figureOfEight();
      world2.figureOfEight();
   }
}