// EnlargingPicturesApplet.java (c) 2000-2006 Kari Laitinen // http://www.naturalprogramming.com/ // 2000-10-22 File created. // 2006-11-14 Last modification. /* This program is an enhanced version of PictureShowApplet.java. 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 EnlargingPicturesApplet 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 ; int applet_width, applet_height ; int applet_center_point_x, applet_center_point_y ; int picture_width_in_applet, picture_height_in_applet ; 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. " ) ; } // The following statements get the dimensions of the area // that is reserved for the applet on the html page. applet_width = getSize().width ; applet_height = getSize().height ; applet_center_point_x = applet_width / 2 ; applet_center_point_y = applet_height / 2 ; } 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() { System.out.print( "\n Method run() started." ) ; 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 ) { int picture_width = pictures_to_be_shown[ index_of_current_picture ].getWidth( this ) ; int picture_height = pictures_to_be_shown[ index_of_current_picture ].getHeight( this ); float picture_ratio = (float) picture_width / (float) picture_height ; int picture_size_shrinker = 10 ; do { picture_width_in_applet = applet_width / picture_size_shrinker ; picture_height_in_applet = (int) (picture_width_in_applet / picture_ratio) ; picture_size_shrinker -- ; repaint() ; try { Thread.sleep( 1000 ) ; // Sleep 1 second. } catch ( InterruptedException caught_exception ) { // No actions to handle the exception. } } while ( picture_size_shrinker > 0 && thread_must_be_executed == true ) ; index_of_current_picture ++ ; } } System.out.print( "\n Method run() finished." ) ; } public void paint( Graphics graphics ) { super.paint( graphics ) ; int picture_left_corner_x = applet_center_point_x - picture_width_in_applet / 2 ; int picture_left_corner_y = applet_center_point_y - picture_height_in_applet / 2 ; graphics.drawImage( pictures_to_be_shown[ index_of_current_picture ], picture_left_corner_x, picture_left_corner_y, picture_width_in_applet, picture_height_in_applet, this ) ; } }