// PictureShowApplet.java (c) 2000-2006 Kari Laitinen // http://www.naturalprogramming.com/ // 2000-10-22 File created. // 2006-11-14 Last modification. /* This program shows how an applet can show pictures and how the downloading of picture files is ensured by using a MediaTracker object. The picture files of various famous paintings have been obtained from the Internet. The copyrights of the pictures are owned by other institutions than the author of this program. Les Demoiselles d'Avignon (1907 by Pablo Picasso is at MOMA (Museum of Modern Art), New York. Mona Lisa (1503-6) by Leonardo da Vinci is at Louvre in Paris, France. Persistence of Memory (1931) by Salvador Dali is at MOMA, New York. The Night Watch (1642) of Rembrandt is at Rijksmuseum, Amsterdam. The 'Yellow Field' painting is made by Vincent van Gogh who lived 1853-1890. */ import java.awt.* ; public class PictureShowApplet extends javax.swing.JApplet implements Runnable { Thread animation_thread ; boolean thread_must_be_executed = false ; Image[] pictures_to_be_shown ; int index_of_current_picture = 0 ; public void init() { String picture_file_names[] = {"yellow_field_by_vincent_van_gogh.jpg", "night_watch_by_rembrandt.jpg", "persistence_of_memory_by_dali.jpg", "demoiselles_de_avignon_by_picasso.jpg", "mona_lisa_by_leonardo.jpg" } ; pictures_to_be_shown = new Image[ picture_file_names.length ] ; // By using a MediaTracker object, we can ensure that all pictures // are loaded from the server before we start showing them. MediaTracker picture_tracker = new MediaTracker( this ) ; // The following loop creates the Image objects as well as adds // them to the MediaTracker object. for ( int picture_index = 0 ; picture_index < picture_file_names.length ; picture_index ++ ) { pictures_to_be_shown[ picture_index ] = getImage( getCodeBase(), picture_file_names[ picture_index ] ) ; picture_tracker.addImage( pictures_to_be_shown[ picture_index ], picture_index ) ; } try { picture_tracker.waitForAll() ; } catch ( InterruptedException caught_exception ) { System.out.print( "\n InterruptedException not handled. " ) ; } } public void start() { if ( animation_thread == null ) { animation_thread = new Thread( this ) ; thread_must_be_executed = true ; animation_thread.start() ; } } public void stop() { if ( animation_thread != null ) { thread_must_be_executed = false ; animation_thread.interrupt() ; animation_thread = null ; } } public void run() { // The followint loops change the value of index_of_current_picture. // This index is used in the paint() method to determine which // picture is to be shown. while ( thread_must_be_executed == true ) { index_of_current_picture = 0 ; while ( index_of_current_picture < pictures_to_be_shown.length && thread_must_be_executed == true ) { repaint() ; try { Thread.sleep( 3000 ) ; // Sleep 3 seconds. } catch ( InterruptedException caught_exception ) { // No actions to handle the exception. } index_of_current_picture ++ ; } } } public void paint( Graphics graphics ) { super.paint( graphics ) ; graphics.drawImage( pictures_to_be_shown[ index_of_current_picture ], 30, 20, this ) ; } }