Download presentation
Presentation is loading. Please wait.
1
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01
2
OutlineOutline ä Applet methods The init() method The start() and stop() methods The run() method The repaint() method ä Threads The sleep() method ä Fun examples ä Applet methods The init() method The start() and stop() methods The run() method The repaint() method ä Threads The sleep() method ä Fun examples
3
Applet Methods We’ve seen the paint() method of the Applet class – it’s used to display stuff to the screen. When is the paint() method called? ä Automatically when the applet is first displayed on the screen. ä Automatically when the window containing the applet is resized. ä Automatically when the applet becomes visible after having been covered by another window. ä Whenever the applet needs to be refreshed for animation purposes. The paint() method can be called a huge number of times. We don’t want to put all of our code in this method – that would slow down the program. We’ve seen the paint() method of the Applet class – it’s used to display stuff to the screen. When is the paint() method called? ä Automatically when the applet is first displayed on the screen. ä Automatically when the window containing the applet is resized. ä Automatically when the applet becomes visible after having been covered by another window. ä Whenever the applet needs to be refreshed for animation purposes. The paint() method can be called a huge number of times. We don’t want to put all of our code in this method – that would slow down the program.
4
The init() Method Another method of the Applet class is the init() method. Like the paint() method, this method is also called automatically. The paint() method is called repeatedly. The init() method only gets called once, when the applet is first loaded into the web browser. Any initialization code that can go in the init() method rather than the paint() method should. For example, setBackground() should go in the init() method. Another method of the Applet class is the init() method. Like the paint() method, this method is also called automatically. The paint() method is called repeatedly. The init() method only gets called once, when the applet is first loaded into the web browser. Any initialization code that can go in the init() method rather than the paint() method should. For example, setBackground() should go in the init() method.
5
The start() and stop() Methods The start() method gets called automatically when the applet becomes active (for example, when the web page is refreshed). The stop() method gets called automatically when the applet becomes inactive (for example, when you go to a different web page). ä There’s certain code that’s required in these methods (see examples). Normally, you won’t want to add any additional code to them. The start() method gets called automatically when the applet becomes active (for example, when the web page is refreshed). The stop() method gets called automatically when the applet becomes inactive (for example, when you go to a different web page). ä There’s certain code that’s required in these methods (see examples). Normally, you won’t want to add any additional code to them.
6
The run() Method The run() method gets called between the start() and stop() methods. Normally there’s a loop inside this method, and the paint() method is called from inside the loop. This repeated calling of the paint() method, where the stuff being painted is slightly different each time, is what produces an animation. run() is not an instance method of the Applet class. To use it, we have to add something else to our class definition: ä public class Animation extends Applet implements Runnable The run() method gets called between the start() and stop() methods. Normally there’s a loop inside this method, and the paint() method is called from inside the loop. This repeated calling of the paint() method, where the stuff being painted is slightly different each time, is what produces an animation. run() is not an instance method of the Applet class. To use it, we have to add something else to our class definition: ä public class Animation extends Applet implements Runnable
7
The repaint() Method Actually, we don’t usually call the paint() method. Rather, we call the repaint() method. paint() will simply display graphics on top of what’s already there. repaint(), on the other hand, clears the graphics context first. It then calls paint(). Actually, we don’t usually call the paint() method. Rather, we call the repaint() method. paint() will simply display graphics on top of what’s already there. repaint(), on the other hand, clears the graphics context first. It then calls paint().
8
Threads and the sleep() Method ä A thread is a single execution of a program. ä Some languages, including Java, allow for multiple threads to run at once. ä That is, you can have multiple program executions going at once. Thread.sleep() tells your execution to sleep, or pause, so that other executions can have a turn. Thread.sleep() takes one argument of type int, indicating how many milliseconds to pause for. Example: Thread.sleep(1000) will pause your program for 1 second. ä A thread is a single execution of a program. ä Some languages, including Java, allow for multiple threads to run at once. ä That is, you can have multiple program executions going at once. Thread.sleep() tells your execution to sleep, or pause, so that other executions can have a turn. Thread.sleep() takes one argument of type int, indicating how many milliseconds to pause for. Example: Thread.sleep(1000) will pause your program for 1 second.
9
HomeworkHomework ä HW7 out today, due Tuesday. ä Tic-Tac-Toe. ä HW7 out today, due Tuesday. ä Tic-Tac-Toe.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.