/* * slideShow.java * * Created on November 2, 2004, 7:18 PM */ /** * @author Sasikaran Loganathan : ID - 021073994 * @author Parvez Akkas : ID - 020928636 */ import javax.swing.* ; import javax.swing.border.TitledBorder ; import javax.swing.border.EtchedBorder ; import java.awt.* ; import java.awt.image.* ; import java.awt.event.* ; import java.net.* ; import java.lang.* ; public class slideShow extends JApplet implements Runnable, ActionListener, MouseListener { public int IMAGE_NUM = 35 ; public Image[] image = new Image[IMAGE_NUM]; public JLabel[] thumbnails = new JLabel [IMAGE_NUM] ; public JLabel[] lblSlideShowImage = new JLabel[IMAGE_NUM] ; public JCheckBox[] chkThumbnail = new JCheckBox[IMAGE_NUM] ; public ImageIcon[] icnThumbnail = new ImageIcon[IMAGE_NUM] ; public ImageIcon[] icnImage = new ImageIcon[IMAGE_NUM] ; public JPanel pnlThumbCheckBox = new JPanel() ; public JPanel pnlThumbnailImage = new JPanel() ; public JPanel pnlSlideShow = new JPanel() ; public JPanel[] pnlSlides = new JPanel[IMAGE_NUM] ; public CardLayout slideShowLayout ; public JButton cmdNextThumbnail ; public JButton cmdPreviousThumbnail ; public JButton cmdAddToSlideShow ; public JButton cmdRemoveFromSlideShow ; public JButton cmdPlaySlideShow ; public JButton cmdStopSlideShow ; public JButton cmdNextSlide ; public JButton cmdSlideShowDelay ; public JTextField txtSlideShowDelay ; public Timer timer ; public Thread prepareImageThread ; public int nextCounter ; public int previousCounter ; public Container content ; /** Initialization method that will be called after the applet is loaded * into the browser. */ public void init() { //WindowUtilities.setNativeLookAndFeel() ; // prepareImages() ; //prepareThumbnails() ; // int imageNum ; // imageNum = Integer.parseInt(getParameter("totalImage")) ; // IMAGE_NUM = imageNum ; // System.out.println("IMAGE_NUM is " + IMAGE_NUM) ; startThread() ; // while(prepareImageThread.isAlive() == false) // { content = getContentPane() ; content.setBackground(Color.BLACK) ; content.add(actionPanel(), BorderLayout.SOUTH) ; //content.add(slideShowPanel(), BorderLayout.CENTER) ; content.add(thumbnailPanel(), BorderLayout.WEST) ; timer = new Timer(2000, this) ; // } // timer.start() ; } public void start() { System.out.println("Going to run Thread now.\n") ; startThread() ; System.out.println("Finished running thread.\n") ; content.add(slideShowPanel(), BorderLayout.CENTER) ; System.out.println("Added the slide show panel in the center.\n") ; } /** * This method creates a JPanel that keeps all the thumbnail images, * the checkboxes and the buttons for browsing through the thumbnails. * Both checkboxes and the thumbnail images are being held into two other * JPanels * @return returns a JPanel that has the thumbnails and the buttons * for thumbnails */ public JPanel thumbnailPanel() { JPanel pnlThumbPanel = new JPanel() ; pnlThumbPanel.setLayout(new BorderLayout()) ; pnlThumbPanel.setBackground(Color.BLACK) ; thumbnailButtonInit() ; pnlThumbPanel.add(cmdPreviousThumbnail, BorderLayout.NORTH) ; pnlThumbPanel.add(thumbnailMiddlePanel(), BorderLayout.CENTER) ; pnlThumbPanel.add(cmdNextThumbnail, BorderLayout.SOUTH) ; // pnlThumbPanel.setBorder(BorderFactory.createTitledBorder("Thumbnails")) ; pnlThumbPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.BLUE), "Thumbnails", TitledBorder.CENTER, TitledBorder.CENTER, new Font("SansSerif", Font.ITALIC, 12), Color.GREEN)) ; return pnlThumbPanel ; } /** * This method creates a JPanel, which contains two other JPanels for * the checkboxes and the thumbnails respectively. * @return returns a JPanel */ public JPanel thumbnailMiddlePanel() { JPanel pnlThumbMidPanel = new JPanel() ; pnlThumbMidPanel.setLayout(new GridLayout(1,2)) ; pnlThumbMidPanel.setBackground(Color.WHITE) ; pnlThumbMidPanel.add(thumbnailCheckBoxPanel()) ; pnlThumbMidPanel.add(thumbnailImagePanel()) ; return pnlThumbMidPanel ; } /** * This method creates checkboxes depending on the number of images that * are available, which can be found from IMAGE_NUM variable. However, * even though it creates all the chechboxes here at the same time, it * only adds the first five checkboxes to the panel, since there can be * only five checkboxes viewed at a time. The rest of the checkboxes are * added to the panel as they are needed to be viewed. * @return returns a JPanel that is keeping the checkboxes */ public JPanel thumbnailCheckBoxPanel() { pnlThumbCheckBox.setLayout(new GridLayout(5, 1)) ; pnlThumbCheckBox.setBackground(Color.BLACK) ; int chkThumbnailLabel ; for(int i = 0; i < chkThumbnail.length ; i++) { chkThumbnailLabel = i + 1 ; chkThumbnail[i] = new JCheckBox(chkThumbnailLabel + ".") ; chkThumbnail[i].setBackground(Color.WHITE) ; chkThumbnail[i].setForeground(Color.BLUE) ; if(i < 5) pnlThumbCheckBox.add(chkThumbnail[i]) ; } return pnlThumbCheckBox ; } /** * This method generates all the images are available, which can be * found from IMAGE_NUM variable. And then those images are put into * a label since a panel cannot hold any images. However, even though * it generates all the images here at the same time, it only adds the * first five images to the panel, since there can be only five checkboxes * viewed at a time. The rest of the checkboxes are added to the panel as * they are needed to be viewed. * @return returns a JPanel that is keeping all the thumbnail images */ public JPanel thumbnailImagePanel() { pnlThumbnailImage.setLayout(new GridLayout(5, 1)) ; //ImageIcon[] icnThumbnail = new ImageIcon[IMAGE_NUM]; pnlThumbnailImage.setBackground(Color.BLACK) ; for(int i = 0 ; i < image.length ; i++) { icnThumbnail[i] = new ImageIcon(image[i].getScaledInstance(75, 75, Image.SCALE_SMOOTH)) ; thumbnails[i] = new JLabel(icnThumbnail[i]) ; thumbnails[i].setBorder(BorderFactory.createLineBorder(Color.GREEN, 1)) ; if(i < 5) pnlThumbnailImage.add(thumbnails[i]) ; } return pnlThumbnailImage ; } public JPanel actionPanel() { JPanel pnlAction = new JPanel() ; pnlAction.setLayout(new GridLayout(1,2)) ; pnlAction.add(addRemovePanel()) ; pnlAction.add(slideShowControlPanel()) ; return pnlAction ; } public JPanel addRemovePanel() { JPanel pnlAddRemove = new JPanel() ; pnlAddRemove.setLayout(new FlowLayout()) ; pnlAddRemove.setBackground(Color.BLACK) ; cmdAddToSlideShow = new JButton("Add To SlideShow") ; cmdRemoveFromSlideShow = new JButton("Remove From SlideShow") ; cmdAddToSlideShow.setBackground(Color.BLACK) ; cmdAddToSlideShow.setForeground(Color.GREEN) ; cmdRemoveFromSlideShow.setBackground(Color.BLACK) ; cmdRemoveFromSlideShow.setForeground(Color.GREEN) ; pnlAddRemove.add(cmdAddToSlideShow) ; pnlAddRemove.add(cmdRemoveFromSlideShow) ; cmdAddToSlideShow.addActionListener(this) ; cmdRemoveFromSlideShow.addActionListener(this) ; // pnlAddRemove.setBorder(BorderFactory.createTitledBorder("Add/Remove")) ; pnlAddRemove.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.BLUE), "Add / Remove", TitledBorder.CENTER, TitledBorder.CENTER, new Font("SansSerif", Font.ITALIC, 12), Color.GREEN)) ; return pnlAddRemove ; } public JPanel slideShowControlPanel() { JPanel pnlSlidesControl = new JPanel() ; pnlSlidesControl.setLayout(new FlowLayout()) ; pnlSlidesControl.setBackground(Color.BLACK) ; JLabel lblGetSlideShowDelay = new JLabel("Time Delay in seconds:") ; txtSlideShowDelay = new JTextField(2) ; cmdPlaySlideShow = new JButton("Play") ; cmdStopSlideShow = new JButton("Stop") ; cmdSlideShowDelay = new JButton("Change") ; cmdPlaySlideShow.setBackground(Color.BLACK) ; cmdPlaySlideShow.setForeground(Color.GREEN) ; cmdStopSlideShow.setBackground(Color.BLACK) ; cmdStopSlideShow.setForeground(Color.GREEN) ; cmdSlideShowDelay.setBackground(Color.BLACK) ; cmdSlideShowDelay.setForeground(Color.GREEN) ; lblGetSlideShowDelay.setForeground(Color.GREEN) ; txtSlideShowDelay.setBorder(BorderFactory.createLineBorder(Color.BLUE, 2)) ; txtSlideShowDelay.setForeground(Color.BLUE) ; pnlSlidesControl.add(cmdPlaySlideShow) ; pnlSlidesControl.add(cmdStopSlideShow) ; pnlSlidesControl.add(lblGetSlideShowDelay) ; pnlSlidesControl.add(txtSlideShowDelay) ; pnlSlidesControl.add(cmdSlideShowDelay) ; cmdPlaySlideShow.addActionListener(this) ; cmdStopSlideShow.addActionListener(this) ; cmdSlideShowDelay.addActionListener(this) ; // pnlSlidesControl.setBorder(BorderFactory.createTitledBorder("Slide Show Controls")) ; pnlSlidesControl.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.BLUE), "Slide Show Controls", TitledBorder.CENTER, TitledBorder.CENTER, new Font("SansSerif", Font.ITALIC, 12), Color.GREEN)) ; return pnlSlidesControl ; } public JPanel slideShowPanel() { slideShowLayout = new CardLayout() ; pnlSlideShow.setLayout(slideShowLayout) ; //ImageIcon[] icnImage = new ImageIcon[IMAGE_NUM] ; pnlSlideShow.setBackground(Color.BLACK) ; for(int i = 0 ; i < image.length ; i++) { //icnImage[i] = new ImageIcon(image[i]) ; lblSlideShowImage[i] = new JLabel(icnImage[i]) ; pnlSlides[i] = new JPanel() ; pnlSlides[i].setBackground(Color.BLACK) ; pnlSlides[i].add(lblSlideShowImage[i]) ; } // pnlSlideShow.setBorder(BorderFactory.createTitledBorder("Slide Show")) ; pnlSlideShow.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.GREEN, Color.BLUE), "Slide Show", TitledBorder.CENTER, TitledBorder.CENTER, new Font("SansSerif", Font.ITALIC, 12), Color.GREEN)) ; return pnlSlideShow ; } public void thumbnailButtonInit() { // Image imageNext = getImage(getDocumentBase(), "images/right.JPG") ; // Image imagePrevious = getImage(getDocumentBase(), "images/left.JPG") ; // ImageIcon next = new ImageIcon("images/right.gif") ; // ImageIcon previous = new ImageIcon("images/left.gif") ; cmdNextThumbnail = new JButton("Next") ; cmdPreviousThumbnail = new JButton("Previous") ; cmdNextThumbnail.addActionListener(this) ; // cmdNextThumbnail.addMouseListener(this) ; cmdPreviousThumbnail.addActionListener(this) ; cmdNextThumbnail.setBackground(Color.BLACK) ; cmdNextThumbnail.setForeground(Color.GREEN) ; cmdPreviousThumbnail.setBackground(Color.BLACK) ; cmdPreviousThumbnail.setForeground(Color.GREEN) ; if(chkThumbnail.length > 5) { cmdNextThumbnail.setEnabled(true) ; } else cmdNextThumbnail.setEnabled(false) ; cmdPreviousThumbnail.setEnabled(false) ; nextCounter = 0 ; previousCounter = 0 ; } public void prepareImages() { // System.out.println("inside the prepareImages() method now") ; try { image[0] = getImage(getDocumentBase(), "../birds/cambodia1995-1.jpg") ; image[1] = getImage(getDocumentBase(), "../birds/cambodia1995-2.jpg") ; image[2] = getImage(getDocumentBase(), "../birds/cambodia1995.jpg") ; image[3] = getImage(getDocumentBase(), "../birds/cuba1994.jpg") ; image[4] = getImage(getDocumentBase(), "../birds/cuba1998-1.jpg") ; image[5] = getImage(getDocumentBase(), "../birds/cuba1998.jpg") ; image[6] = getImage(getDocumentBase(), "../birds/kampuchea1984.jpg") ; image[7] = getImage(getDocumentBase(), "../birds/philippin-1.jpg") ; image[8] = getImage(getDocumentBase(), "../birds/philippin-2.jpg") ; image[9] = getImage(getDocumentBase(), "../birds/philippin-3.jpg") ; image[10] = getImage(getDocumentBase(), "../birds/philippin-4.jpg") ; image[11] = getImage(getDocumentBase(), "../birds/philippin-5.jpg") ; image[12] = getImage(getDocumentBase(), "../birds/philippin.jpg") ; image[13] = getImage(getDocumentBase(), "../birds/sahara1994-1.jpg") ; image[14] = getImage(getDocumentBase(), "../birds/sahara1994-2.jpg") ; image[15] = getImage(getDocumentBase(), "../birds/sahara1994-3.jpg") ; image[16] = getImage(getDocumentBase(), "../birds/sahara1994-4.jpg") ; image[17] = getImage(getDocumentBase(), "../birds/sahara1994.jpg") ; image[18] = getImage(getDocumentBase(), "../birds/sahara1998-1.jpg") ; image[19] = getImage(getDocumentBase(), "../birds/sahara1998-2.jpg") ; image[20] = getImage(getDocumentBase(), "../birds/sahara1998-3.jpg") ; image[21] = getImage(getDocumentBase(), "../birds/sahara1998-4.jpg") ; image[22] = getImage(getDocumentBase(), "../birds/sahara1998.jpg") ; image[23] = getImage(getDocumentBase(), "../birds/somalia1999-1.jpg") ; image[24] = getImage(getDocumentBase(), "../birds/somalia1999-2.jpg") ; image[25] = getImage(getDocumentBase(), "../birds/somalia1999-3.jpg") ; image[26] = getImage(getDocumentBase(), "../birds/somalia1999-4.jpg") ; image[27] = getImage(getDocumentBase(), "../birds/somalia1999.jpg") ; image[28] = getImage(getDocumentBase(), "../birds/unknown1975-1.jpg") ; image[29] = getImage(getDocumentBase(), "../birds/unknown1975.jpg") ; image[30] = getImage(getDocumentBase(), "../birds/unknown1997-1.jpg") ; image[31] = getImage(getDocumentBase(), "../birds/unknown1997-2.jpg") ; image[32] = getImage(getDocumentBase(), "../birds/unknown1997.jpg") ; image[33] = getImage(getDocumentBase(), "../birds/unknown-1.jpg") ; image[34] = getImage(getDocumentBase(), "../birds/unknown.jpg") ; // String paramValue ; // String paramName ; // System.out.println("getting the images from the html file now") ; // paramValue = getParameter("0") ; // image[0] = getImage(getDocumentBase(),paramValue) ; // for(int i = 0 ; i < image.length ; i++) // { // paramName = String.valueOf(i) ; // System.out.println(i + ". parameter name is " + paramName) ; // paramValue = getParameter(paramName) ; // System.out.println("parameter value is " + paramValue) ; // image[i] = getImage(getDocumentBase(), paramValue) ; // } } catch(Exception e) { System.out.println("Bad URL: " + e) ; } MediaTracker imageTracker = new MediaTracker(this) ; for(int i = 0 ; i < image.length ; i++) { imageTracker.addImage(image[i], 0) ; //prepareImage(image[i], chkThumbnail[i]) ; //System.out.println("the value of i is " + i + "\n") ; icnImage[i] = new ImageIcon(image[i]) ; } try { imageTracker.waitForAll() ; } catch(InterruptedException ie) { System.out.println("Error loading images.\n") ; } } /** public void prepareThumbnails() { // int width, height ; MediaTracker thumbnailTracker = new MediaTracker(this) ; for(int i = 0 ; i < image.length ; i++) { // width = image[i].getWidth(this)/5 ; // height = image[i].getHeight(this)/5 ; thumbnailTracker.addImage(image icnThumbnail[i] = new ImageIcon(image[i].getScaledInstance(75, 75, Image.SCALE_SMOOTH)) ; } } */ public void cmdNextThumbnailActionPerformed() { int i ; for(i = nextCounter ; i < nextCounter + 5; i++) { if(i == chkThumbnail.length) break ; else { pnlThumbCheckBox.remove(chkThumbnail[i]) ; pnlThumbnailImage.remove(thumbnails[i]) ; } } nextCounter = nextCounter + 5 ; previousCounter = previousCounter + 5 ; for(i = nextCounter ; i < nextCounter + 5 ; i++) { if(i == chkThumbnail.length) break ; else { pnlThumbCheckBox.add(chkThumbnail[i]) ; pnlThumbnailImage.add(thumbnails[i]) ; } } if(nextCounter + 5 >= chkThumbnail.length) cmdNextThumbnail.setEnabled(false) ; if(cmdPreviousThumbnail.isEnabled() == false) cmdPreviousThumbnail.setEnabled(true) ; //pnlThumbCheckBox.invalidate() ; //pnlThumbCheckBox.validate() ; //pnlThumbnailImage.validate() ; //content.validate() ; pnlThumbCheckBox.setVisible(false) ; pnlThumbCheckBox.setVisible(true) ; pnlThumbnailImage.setVisible(false) ; pnlThumbnailImage.setVisible(true) ; } public void cmdPreviousThumbnailActionPerformed() { int i ; for(i = previousCounter ; i < previousCounter + 5 ; i++) { if(i == chkThumbnail.length) break ; else { pnlThumbCheckBox.remove(chkThumbnail[i]) ; pnlThumbnailImage.remove(thumbnails[i]) ; } } //content.validate() ; //pnlThumbCheckBox.validate() ; previousCounter = previousCounter - 5 ; nextCounter = nextCounter - 5 ; for(i = previousCounter ; i < previousCounter + 5 ; i++) { pnlThumbCheckBox.add(chkThumbnail[i]) ; pnlThumbnailImage.add(thumbnails[i]) ; } if(previousCounter - 5 < 0) cmdPreviousThumbnail.setEnabled(false) ; if(cmdNextThumbnail.isEnabled() == false) cmdNextThumbnail.setEnabled(true) ; //pnlThumbCheckBox.invalidate() ; //pnlThumbCheckBox.validate() ; // pnlThumbCheckBox.hide() ; // pnlThumbCheckBox.show() ; //content.validate() ; pnlThumbCheckBox.setVisible(false) ; pnlThumbCheckBox.setVisible(true) ; pnlThumbnailImage.setVisible(false) ; pnlThumbnailImage.setVisible(true) ; } public void cmdAddToSlideShowActionPerformed() { // timer.stop() ; for(int i = 0 ; i < IMAGE_NUM ; i++) { if(chkThumbnail[i].isSelected() == true) { pnlSlideShow.add(pnlSlides[i], "i") ; //pnlSlideShow.add(lblSlideShowImage[i]) ; chkThumbnail[i].setVisible(false) ; thumbnails[i].setVisible(false) ; } } slideShowLayout.last(pnlSlideShow) ; // timer.start() ; } public void cmdRemoveFromSlideShowActionPerformed() { int i ; timer.stop() ; for(i = 0 ; i < IMAGE_NUM ; i++) { if(pnlSlides[i].isShowing()) { pnlSlideShow.remove(pnlSlides[i]) ; chkThumbnail[i].setSelected(false) ; chkThumbnail[i].setVisible(true) ; thumbnails[i].setVisible(true) ; pnlSlideShow.setVisible(false) ; pnlSlideShow.setVisible(true) ; } } slideShowLayout.last(pnlSlideShow) ; timer.start() ; } public void cmdNextThumbnailSlideActionPerformed() { slideShowLayout.next(pnlSlideShow) ; } public void cmdPlaySlideShowActionPerformed() { timer.start() ; } public void cmdStopSlideShowActionPerformed() { timer.stop() ; } public void cmdSlideShowDelayActionPerformed() { String text ; int delay ; text = txtSlideShowDelay.getText() ; timer.stop() ; try { delay = Integer.parseInt(text) ; if(delay > 0) timer.setDelay(delay * 1000) ; } catch(NumberFormatException nfp) { System.out.println("Number Format Exception thrown") ; } timer.start() ; } public void startThread() { System.out.println("Creating Thread object.\n") ; prepareImageThread = new Thread(this) ; prepareImageThread.start() ; System.out.println("The run method of the Thread has been called.\n") ; } public void run() { System.out.println("Thread Started.\n") ; prepareImages() ; System.out.println("Thread Stopped.\n") ; } public void actionPerformed(ActionEvent e) { if(e.getSource() == cmdNextThumbnail) { cmdNextThumbnailActionPerformed() ; } else if(e.getSource() == cmdPreviousThumbnail) { cmdPreviousThumbnailActionPerformed() ; } else if(e.getSource() == cmdAddToSlideShow) { cmdAddToSlideShowActionPerformed() ; } else if(e.getSource() == cmdRemoveFromSlideShow) { cmdRemoveFromSlideShowActionPerformed() ; } else if(e.getSource() == cmdNextSlide) { cmdNextThumbnailSlideActionPerformed() ; } else if(e.getSource() == timer) { slideShowLayout.next(pnlSlideShow) ; } else if(e.getSource() == cmdPlaySlideShow) { cmdPlaySlideShowActionPerformed() ; } else if(e.getSource() == cmdStopSlideShow) { cmdStopSlideShowActionPerformed() ; } else if(e.getSource() == cmdSlideShowDelay) { cmdSlideShowDelayActionPerformed() ; } } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }