Image Observer Example
Now let's look at a practical example that overrides image Update( ) to make a version I" of the Simple Image Load applet that doesn't flicker as much. The default implementation of image Update( ) in Applet has.several problems. First, it repaints the entire image each time any new data arrives. This causes flashing between the background color and the image. Second, it uses a feature of Apple Repaint () to cause the system to only repaint'the image every tenth of a second or so. This causes a jerky uneven feel as the image is painting: Finally, the default implementation knows nothing about images that may fan to load properly. Many beginning Java 'programmers are frustrated b)f the fact that get Image( ) always succeeds' even when the,image specified does not exist;,You don't find out about missing images until  image Update ( ) occurs. If you Use the default implementation of image Update( ) then you'll never know what happened. Y0u paint( ) method well supply do, nothing when you call draw Image ( ) The example that follows fixes all three of these problems in ten lines of code. First it eliminates the flickering with two small changes. It overrides update( ) so that it calls paint( ) without painting the background color first. The background is set vitae Background( ) in init ( ) so the ,initial color is painted just once. Also, it uses a version of repaint( ) that specifies the rectangle which to paint. The system will set 'the clipping area such that nothing outside of this gel is painted. This reduces repaint tucker and improves performance.
Second, it eliminates the jerky, uneven display of Ute Incoming image by painting every time it receives an update. These updates occur On a scan line-by-scan line basis so an image ,that is 100 pixels tall will be "repainted" 100 times as it loads; Note that  this'is not, the fastest way to display an image, just the smooth st Finally, it handles the error caused by the desired file not being found by examining the flags parameter for the ABORT bit. If it is set, the instance variable error is set to true and then repaint( ) is called. The paint( ) method is modified to print an error message over a bright red background if error is true.

Here is the code.The top screen shows the image half loaded, and the bottom screen displays a filename that has been mistyped in the applet tag Here is an interesting variation of image Update( ) you might want to try. It waits until the image is completely loaded before snapping it onto the screen in a single repaint.

Share This