threads and multimedia animation, images, sound. animation nanimation, displaying a sequence of...

19
Threads and Multimedia Animation, Images, Sound

Upload: adrian-heath

Post on 29-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

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

Threads and Multimedia

Animation, Images, Sound

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

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

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

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)

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

Wrong Approachpublic 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

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

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??

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Tracking ImagesMediaTracker

- 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)

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

Tracking ImagesMore 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

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

The Class URLUsed for accessing web sitesMethods: 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

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

URL ClassMethods, 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

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

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.

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

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)

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

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