Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 - ButtonToMove.java - MoveBall.java - AnimateBall.java 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) 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 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?? 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 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 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 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 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 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 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 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) 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 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 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 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. 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) 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