Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical.

Similar presentations


Presentation on theme: "Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical."— Presentation transcript:

1 Threads and Multimedia Animation, Images, Sound

2 Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical application of threads nOne thread is used to download the image(s) nOther thread(s) are used to paint the image(s) sequentially nSee programs -ButtonToMove.java -MoveBall.java -AnimateBall.java

3 ButtonToMove nThis program uses a button to allow the user to move a ball across the screen -Push button, move ball 9 pixels southeast nSolution to animate? -put movement into for loop (next slide)

4 Wrong Approach public void actionPerformed(ActionEvent 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 actionPerformed method because the calls to repaint will be combined into one call to paint. effect is to show only position at end of loop

5 MoveBall.java nAnimates ball using a separate thread nThe button pressed creates the thread to animate ball. nIn an applet, the applet will implement runnable and the code for the run method will be used by the thread. -why does it have to implement runnable? nSee actionPerformed in MoveBall.java -try pushing the Move button multiple times Why does it act the way it does??

6 AnimateBall.java nModified MoveBall 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. nSee start, stop, and run of AnimateBall.java

7 Line Animation nUses a thread to move lines across an applet nThe thread draws several lines repeatedly to create the illusion of the line moving. nWhen the lines reach the border of the applet, the motion reverses. nSee LineAnimation.java

8 Text Animation nDemonstrates how a streaming banner can be shown in an applet. nApplet with string drawn inside -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 nAnimation.java

9 The Clock Frame nDemonstrates Frames, Event Handler, Threads, and Calendar class to build a clock with alarm. nThread is used to run the clock nCan embed in another application nApplication: ClockFrame.java -Note use of Toolkit

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

11 Images in Applets Some Methods needed -getDocumentBase() -getCodeBase() -getImage(url, filename) im.getWidth(this); im.getHeight(this); -g.drawImage(im, x, y, this); there are several versions of drawImage Example: ImageDemo.java

12 Images and URLs ngetCodeBase() -location of applet code ngetDocumentBase() -location of document in which applet is embedded ngetImage(URL,Filename) -load named image from given URL -returns the image in a separate thread (Image is, of course, a class) ndrawImage(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

13 Tracking Images MediaTracker -Keeps track of images added to its list Methods: naddImage(Image, id) -add an image for this media tracker to track id is passed in and used to identify the image nremoveImage(Image) -stop tracking the named image (why not its id?) ncheckAll() -returns true if all tracked images are done loading ncheckID(id) -returns true if all tracked images with this id are done loading (can be multiples)

14 Tracking Images More MediaTracker methods: nisErrorID(id) -check error status of images with given id; return true if error nisErrorAny() -check error status of all tracked images nwaitForID(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 nwaitForAll(id,[ms]) -same as waitForID, but starts loading all images

15 The Class URL Used for accessing web sites Methods: nConstructors; URL(string url) -simplest; 5 other variations on constructor ngetContent(); -retrieves url’s content as Object nopenConnection(); -get a connection to this URL. nopenStream(); -returns InputStream used to read from the URL

16 URL Class Methods, con’t.: ngetHost() -return host as IPv6 address enclosed in square brackets ngetFile() -get actual file this URL represents. Will not have web address if loacl ngetProtocol() -returns http or https, as a string ngetPort() -returns the port number allocated to this URL object ntoExternalForm() -returns string form for this URL

17 Images in Applications nNo applet limitations as to where the image can come from nmust use different methods since application is not an applet ncreate a URL object for the file nload whatever the URL object refers to nwe use getContent() to return an Object, since a URL can refer to any type of Object. If it is an image, getContent returns an ImageProducer object which can create an Image. nAlternative: Use the Toolkit version.

18 Image Demo Applications Examples: nImageDemo2.java -Uses the URL and getContent() with ImageProducer nImageDemo3.java -Use the Toolkit class (actually, Image2Demo did, too; This example uses the toolkit to obtain the image)

19 Sequences of Images nSequence of displayed images create animation. nAnimation can flicker because of repaint. -Avoid by creating ImageIcon nOverride the update method nUse double buffering -draw image twice nExamples: -ImageAnimation.java -AnimateImage_NoFlicker.java


Download ppt "Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical."

Similar presentations


Ads by Google