Discussion of Activity 8.3 - Testing concurrent animation

Sample solution

Code for amended class FlyingLogo:

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

   //amended constructor
   public FlyingLogo(String title, int xPos, int yPos)
   {
      super(title);
      setSize(FRAME_WIDTH,FRAME_HEIGHT);
      setLocation(xPos,yPos);
      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);
      setVisible(true);
   }

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

   public void run()
   {
      squareMove();
      figureOfEight();
   }

   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:

public class FlyingLogoTest
{
   public static void main (String[] args)
   {
      FlyingLogo world =
         new FlyingLogo("Open in space 1",100,100);
      FlyingLogo world2 =
         new FlyingLogo("Open in space 2",500,100);

      Thread t1 = new Thread(world);
      Thread t2 = new Thread(world2);
      t1.start();
      t2.start();
   }
}