Activity 8.3 - Testing concurrent animation

Topic

In this activity we alter the program created in Activity 7.5 so that two animations run concurrently.

Materials

In addition to the Solution document, the project for this activity contains four classes:

Task

When running this project it produces a pair of frames, arranged side by side, containing animated logos.

The logo in the left-hand frame performs a series of movements, then halts, whereupon the logo in the right-hand frame sets off and executes the same sequence of manoeuvres.

If you inspect the main method in FlyingLogoTest you will see that the motion of each logo consists of an invocation of method squareMove, followed by an invocation of figureOfEight.

As the program stands, the two logos are both animated one after another by the same thread of execution. The task is to reorganise it so that the two logos will move at the same time on two separate threads executing concurrently.

Instructions

Compile and run the program, setting ex8_3.FlyingLogoTest as the main class. You should see two frames with logos in. The logo in the left-hand frame will set off and move in a square, then a figure eight, and finally stop. Only at that point will the logo in the right-hand frame begin moving.

You need to make the changes described below, in sequence, testing each alteration where indicated.

  1. In class FlyingLogo modify the constructor FlyingLogo(String title) so that it takes two int arguments and uses them to set the location of the FlyingLogo frame. The signature of the constructor should now be FlyingLogo(String, int, int).

    Alter main() so that it constructs two FlyingLogo instances, one located at (100,100), the other at (500,100), like this:

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

    Recompile and execute the program. At this stage it should still run exactly as before.

  2. The next step is to make the FlyingLogo frames responsible for displaying themselves. Delete the two invocations setVisible(true) that appear in the main method of FlyingLogoTest and simply add setVisible(true) to the FlyingLogo constructor, making sure it is the last statement in the constructor.

    This version of the program should still produce the same result as previously. Test it before going on to the next stage.

  3. In the main method of FlyingLogoTest there are invocations of two methods on each FlyingLogo object, e.g.

    world.squareMove();
    world.figureOfEight();
    

    The next step is to delete these invocations and add a public void run() method to FlyingLogo which will call the two methods concerned (now the invocation will be on this, so for example the first call in run() will take the form this.squareMove(), or if we prefer just squareMove(), since Java lets us omit this.)

  4. In order to test what you have done so far you can temporarily insert the calls

    world.run();
    world2.run();

    into main(). If the program is now executed it should behave exactly as before. All being well, you can then remove these lines and proceed to the final stage.

  5. Alter the declaration of FlyingLogo to

    public class FlyingLogo extends JFrame implements Runnable

    Now in main() write code which will create two threads t1 and t2 with world and world2 as arguments, and start these threads.

    Run the program. The logos should now move concurrently!