Threads and Multimedia Animation Images Sound Animation n

  • Slides: 19
Download presentation
Threads and Multimedia Animation, Images, Sound

Threads and Multimedia Animation, Images, Sound

Animation n n Animation, displaying a sequence of frames to create the illusion of

Animation n n Animation, displaying a sequence of frames to create the illusion of motion, is a typical application of threads One thread is used to download the image(s) Other thread(s) are used to paint the image(s) sequentially See programs - Button. To. Move. java - Move. Ball. java - Animate. Ball. java

Button. To. Move n This program uses a button to allow the user to

Button. To. Move n This program uses a button to allow the user to move a ball across the screen - Push button, move ball 9 pixels southeast n Solution to animate? - put movement into for loop (next slide)

Wrong Approach public void action. Performed(Action. Event ae) { for(int i = 0; i

Wrong Approach public void action. Performed(Action. Event ae) { for(int i = 0; i < 10; i++) { x += 9; y += 9; repaint(); // system collects them into // one call to paint() } } - Cannot put motion in a for loop in the action. Performed method because the calls to repaint will be combined into one call to paint. • effect is to show only position at end of loop

Move. Ball. java Animates ball using a separate thread n The button pressed creates

Move. Ball. java Animates ball using a separate thread n The button pressed creates the thread to animate ball. n In an applet, the applet will implement runnable and the code for the run method will be used by the thread. n - why does it have to implement runnable? n See action. Performed in Move. Ball. java - try pushing the Move button multiple times • Why does it act the way it does? ?

Animate. Ball. java Modified Move. Ball so that the run method continually moves the

Animate. Ball. java Modified Move. Ball so that the run method continually moves the ball until the web page is left. When the web page is re-entered, the thread will start again. n See start, stop, and run of Animate. Ball. java n

Line Animation Uses a thread to move lines across an applet n The thread

Line Animation Uses a thread to move lines across an applet n The thread draws several lines repeatedly to create the illusion of the line moving. n When the lines reach the border of the applet, the motion reverses. n See Line. Animation. java n

Text Animation Demonstrates how a streaming banner can be shown in an applet. n

Text Animation Demonstrates how a streaming banner can be shown in an applet. n Applet with string drawn inside n - no init() - no nothing, except a graphics object used to draw a string • see what happens when you change its size in the html file n Animation. java

The Clock Frame Demonstrates Frames, Event Handler, Threads, and Calendar class to build a

The Clock Frame Demonstrates Frames, Event Handler, Threads, and Calendar class to build a clock with alarm. n Thread is used to run the clock n Can embed in another application n Application: Clock. Frame. java n - Note use of Toolkit

Images n n Applets and applications can use images in the. gif or. jpg

Images n n Applets and applications can use images in the. gif or. jpg format Applets usually cannot read images from the client computer even if the page came from the client. images usually are in the base path of the web page or one of its subdirectories. images use a URL to find them

Images in Applets Some Methods needed - get. Document. Base() - get. Code. Base()

Images in Applets Some Methods needed - get. Document. Base() - get. Code. Base() - get. Image(url, filename) • im. get. Width(this); • im. get. Height(this); - g. draw. Image(im, x, y, this); • there are several versions of draw. Image Example: Image. Demo. java

Images and URLs n get. Code. Base() - location of applet code n get.

Images and URLs n get. Code. Base() - location of applet code n get. Document. Base() - location of document in which applet is embedded n get. Image(URL, Filename) - load named image from given URL - returns the image in a separate thread (Image is, of course, a class) n draw. Image(image, x, y, which) - place the named image, with its upper left-hand corner at the given point. - which is the object to be notified as drawing progresses whether it is progressing - returns false if drawing is still occurring when it returns

Tracking Images Media. Tracker - Keeps track of images added to its list Methods:

Tracking Images Media. Tracker - Keeps track of images added to its list Methods: n add. Image(Image, id) - add an image for this media tracker to track • id is passed in and used to identify the image n remove. Image(Image) - stop tracking the named image (why not its id? ) n check. All() - returns true if all tracked images are done loading n check. ID(id) - returns true if all tracked images with this id are done loading (can be multiples)

Tracking Images More Media. Tracker methods: n is. Error. ID(id) - check error status

Tracking Images More Media. Tracker methods: n is. Error. ID(id) - check error status of images with given id; return true if error n is. Error. Any() - check error status of all tracked images n wait. For. ID(id, [ms]) - Starts loading all images tracked by this media tracker with the specified identifier. Can specify maximum time in milliseconds to wait for loading to complete n wait. For. All(id, [ms]) - same as wait. For. ID, but starts loading all images

The Class URL Used for accessing web sites Methods: n Constructors; URL(string url) -

The Class URL Used for accessing web sites Methods: n Constructors; URL(string url) - simplest; 5 other variations on constructor n get. Content(); - retrieves url’s content as Object n open. Connection(); - get a connection to this URL. n open. Stream(); - returns Input. Stream used to read from the URL

URL Class Methods, con’t. : n get. Host() - return host as IPv 6

URL Class Methods, con’t. : n get. Host() - return host as IPv 6 address enclosed in square brackets n get. File() - get actual file this URL represents. Will not have web address if loacl n get. Protocol() - returns http or https, as a string n get. Port() - returns the port number allocated to this URL object n to. External. Form() - returns string form for this URL

Images in Applications n n n No applet limitations as to where the image

Images in Applications n n n No applet limitations as to where the image can come from must use different methods since application is not an applet create a URL object for the file load whatever the URL object refers to we use get. Content() to return an Object, since a URL can refer to any type of Object. If it is an image, get. Content returns an Image. Producer object which can create an Image. Alternative: Use the Toolkit version.

Image Demo Applications Examples: n Image. Demo 2. java - Uses the URL and

Image Demo Applications Examples: n Image. Demo 2. java - Uses the URL and get. Content() with Image. Producer n Image. Demo 3. java - Use the Toolkit class • (actually, Image 2 Demo did, too; This example uses the toolkit to obtain the image)

Sequences of Images n n Sequence of displayed images create animation. Animation can flicker

Sequences of Images n n Sequence of displayed images create animation. Animation can flicker because of repaint. - Avoid by creating Image. Icon n n Override the update method Use double buffering - draw image twice n Examples: - Image. Animation. java - Animate. Image_No. Flicker. java