Copyright © – Sudharsan Iyengar

 

CS – 410         Lab      1/18                             Assignment – Due 1/23

 

MVC, Event-Driven Programming, Animation

 

First, there were the silent movies. Then came the techni-color movies and talkies. We thought that was it. Walt Disney created an entire new paradigm of entertainment – Cartoons or Animation. And then Video Games.

 

The principle of computer animation is no different than that used in the early cartoons. You need to display a continuous stream of images – each one slightly different from the earlier one fast enough to reflect motion. The quality of your animation will greatly depend on your images and the speed of display.

 

This can be accomplished through a loop in the listener that displays the images in succession with a brief pause. The pause can be accomplished by the statement

 

            Thread.currentThread().sleep(sleeptime);  // sleeptime is an int and represents time in milliseconds

 

Demonstrate the use of this feature by redrawing a window on the screen with different size (or relocating the window) sleeping about 1000 milliseconds between displays. 

 

 

Display different images within a frame to simulate movement. Demonstrate this by displaying different images on a label with pauses between displays.

 

 

MVC is pattern of software design– where the View (UI) is developed independent of the Model (Business Data Handler) and the Controller (Listener) acts as the liaison between the two. The Controller receives input from and handles changes to the UI. Each of these is developed independently.

 

For the purposes of flexible and adaptable animation, we develop a separate unit – that handles animation. It is registered with, and receives instructions and access from, the Controller. This Controller can respond to synchronous events (through Timers) as well as asynchronous events triggered through buttons or mouse.

 

Timers are available in Java. They are event generators and are registered with an ActionListener. Timers are created with two parameters – a strobe delay (int) and a listener. After every delay an event is generated and actionPerformed on the listener invoked. Demonstrate a Timer Based Animation with a periodic display of circles (of different sizes).

 

Additionally, we may want to control – for example: start and stop - the animation. The listener cannot effectively present the continuous display of images for effective animation and at the same time respond to asynchronous events from the UI. Therefore, the listener delegates the responsibility for animation to a separate process of execution – let us call it the Animator.

 

The Animator is responsible for the animation aspect of the UI. The listener now can respond to events as before.

 

 

Develop an application where a window has a panel in which a filled ball can be animated to move. There should be four buttons on the screen. Two (2) buttons for setting the direction of the movement of the ball – labeled Left-Right and Up-Down. The movement of the ball should be based on which button is last clicked. If the last button clicked is Left-Right then the ball moves left-to-right or right-to-left (turning back when it reaches the border). If the last button clicked is Up-Down, then the ball moves up-to-down and down -to-up (turning back when it reaches the border).

 

Have a stop button to stop the animation. Clicking on the start button should start the animation.

 

Individual Assignment:

 

Include in this the ability to use your arrow buttons to control the direction of the ball movement – left, right, up, and down. Now include obstacle(s) that move in the frame. If the ball encounters the obstacle(s) the user loses. Create a scoring system.