// JAVA TEST CODES FOR DISPLAYING PICTURES ONE AT A TIME /* To use, copy seven (7) jpg files into an empty folder and save this program into the folder as PictureViewer.java. Change the filenames in the images and comments string arays below into the file names of your jpg files. (Choose imaginative comments! Compile the program using the command javac PictureViewer.java. Create an html file with the following contents: and save the file as PictureViewer.html. Run the program using the command appletviewer PictureViewer.html. */ /* The program uses one thread to control the timing of picture exposure. Since the class we're using already inherited from JApplet, we just implement the Runnable interface. An aray is used to contain the filenames of the files to be displayed and the comments appearing at the bottom. We use the BorderLayout layout to center the image vertically and to place the title and comments on top and bottom of page respectively. An array index (imageCounter) is used to synchronize the exposure of picture and comments. We start the thread in the init method and override the thread's run method with a call to our display method which is responsible for displaying the correct picture and comments. The thread's sleep method controls how long the picture is displayed. */ import javax.swing.*; import java.awt.*; public class PictureViewer extends JApplet implements Runnable { Font myFont; JLabel lblTitle; JLabel lblLeftMargin; JLabel lblRightMargin; JLabel lblImages; JLabel lblNames; ImageIcon imgIcon; String images[] = {"Cliff.jpg", "Diagonal.jpg", "Iceberg.jpg", "Shed.jpg", "Smokey.jpg", "Vinca.jpg", "Yosemite.jpg"}; String comments[] = {"Cliff.jpg", "Diagonal.jpg", "Iceberg.jpg", "Shed.jpg", "Smokey.jpg", "Vinca.jpg", "Yosemite.jpg"}; int imageCounter; Thread imagesThread; public void init() { lblTitle = new JLabel("My Java Picture Viewer"); lblLeftMargin = new JLabel(" "); lblRightMargin = new JLabel(" "); lblImages = new JLabel(" "); lblNames = new JLabel(" "); Container content = getContentPane(); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); myFont = new Font("SanSerif", Font.BOLD, 16); lblTitle.setHorizontalAlignment(JLabel.CENTER); lblTitle.setFont(myFont); lblImages.setHorizontalAlignment(JLabel.CENTER); lblNames.setHorizontalAlignment(JLabel.CENTER); panel.add(lblTitle, BorderLayout.NORTH); panel.add(lblLeftMargin, BorderLayout.WEST); panel.add(lblRightMargin, BorderLayout.EAST); panel.add(lblImages, BorderLayout.CENTER); panel.add(lblNames, BorderLayout.SOUTH); content.add(panel); imagesThread = new Thread(this); imagesThread.start(); } public void run() { for(;;) { displayImages(); try { imagesThread.sleep(3000); } catch(InterruptedException e) { showStatus("Thread Interrupted"); } } } public void displayImages() { imgIcon = new ImageIcon(images[imageCounter]); lblImages.setIcon(imgIcon); myFont = new Font("SansSerif", Font.BOLD, 16); lblNames.setFont(myFont); lblNames.setText(comments[imageCounter]); imageCounter++; if(imageCounter >= 7) { imageCounter = 0; } } } /* Personalize the program by editing the string pointed to by the lblTitle variable above into something like "MyName's Java Picture Viewer". Send questions to apcastro111@yahoo.com. */