1 / 19

Threads and Multimedia

Threads and Multimedia. Animation, Images, Sound. Animation. 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

bina
Download Presentation

Threads and Multimedia

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threads and Multimedia Animation, Images, Sound

  2. Animation • 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 • ButtonToMove.java • MoveBall.java • AnimateBall.java

  3. ButtonToMove • This program uses a button to allow the user to move a ball across the screen • Push button, move ball 9 pixels southeast • Solution 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 • Animates ball using a separate thread • The button pressed creates the thread to animate ball. • In 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? • See actionPerformed in MoveBall.java • try pushing the Move button multiple times • Why does it act the way it does??

  6. AnimateBall.java • Modified 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. • See start, stop, and run of AnimateBall.java

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

  8. Text Animation • Demonstrates how a streaming banner can be shown in an applet. • Applet 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 • Animation.java

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

  10. Images • 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

  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 • getCodeBase() • location of applet code • getDocumentBase() • location of document in which applet is embedded • getImage(URL,Filename) • load named image from given URL • returns the image in a separate thread (Image is, of course, a class) • drawImage(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: • addImage(Image, id) • add an image for this media tracker to track • id is passed in and used to identify the image • removeImage(Image) • stop tracking the named image (why not its id?) • checkAll() • returns true if all tracked images are done loading • checkID(id) • returns true if all tracked images with this id are done loading (can be multiples)

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

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

  16. URL Class Methods, con’t.: • getHost() • return host as IPv6 address enclosed in square brackets • getFile() • get actual file this URL represents. Will not have web address if loacl • getProtocol() • returns http or https, as a string • getPort() • returns the port number allocated to this URL object • toExternalForm() • returns string form for this URL

  17. Images in Applications • 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 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. • Alternative: Use the Toolkit version.

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

  19. Sequences of Images • Sequence of displayed images create animation. • Animation can flicker because of repaint. • Avoid by creating ImageIcon • Override the update method • Use double buffering • draw image twice • Examples: • ImageAnimation.java • AnimateImage_NoFlicker.java

More Related