Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Thread for Animation. Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs.

Similar presentations


Presentation on theme: "Using Thread for Animation. Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs."— Presentation transcript:

1 Using Thread for Animation

2 Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs or operations at one time What is happening is that each program or operation is running on its own THREAD This is so the computer can control each thread giving one more priority than another and stopping or starting separate threads independently

3 Animation Classic animation is accomplished by rapidly displaying a sequence of images Each image is slightly changed When done correctly this tricks our brain into seeing movement Another way to look at it is if we take an image –display it –erase it –move the image –display it again –Do this over and over

4 Using Threads for Animation We could probably accomplish some basic animations by putting everything in an infinite while loop We will rather use threads for animation because it give us more control than just a giant while loop would

5 Threads 1 Let’s look at what is needed in the class header and variable section public class PicMover extends Applet implements Runnable { Thread runner; implement Runnable declare a Thread object

6 Threads 2a There are three methods used to control threads // Initialize the thread in the start() method public void start() { if (runner==null) { runner=new Thread(this); runner.start(); } The start() method is where the thread is initialized. It checks if the runner is initialized and if it isn’t it initializes it

7 Threads 2b // The run() method is where you put most of your program public void run() { while (true) { repaint(); //call to redraw the screen // other commands for your program // would go here } This is the method that gets repeated. Therefore this is where you will probably put the bulk of your program

8 Threads 2c //stop() method used to stop the thread public void stop() { if (runner!=null) { runner.stop(); runner=null; } This is the method that is called when the thread needs to be stopped. It checks to see if there is a thread running then stops it if it is running

9 Threads 3 You can “pause” or “unpause” your animation by SUSPENDING or RESUMING your thread The two methods used to do this could be called after a button event occurred: public void actionPerformed(ActionEvent event) { buttonLabel=event.getActionCommand(); if (buttonLabel=="Suspend") { runner.suspend(); } else if (buttonLabel=="Resume") { runner.resume(); }


Download ppt "Using Thread for Animation. Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs."

Similar presentations


Ads by Google