/* ***************************************************************************
* SlideShow by dshanby. Replication of this code without written permission  *
* of the author is prohibited. This program preforms a slide show in which   *
* buttons control the forward and reverse functions of the slide "projector" *
* Picure files must be labeled g0.jpg ~ gN.jpg and saved to the root         *
* directory. The first picture (g0.jpg) should be blank. The MaxNumberofPics *
* variable should be set to the number of pictures N plus one (N+1).         *
*                                                                            *
* Copyright May 28, 1999                                                     *
* Updated August 22, 2000                                                    *
*****************************************************************************
*/

public class SlideShow extends Applet implements ActionListener
{
    Button StartShowButton = new Button("Start Slide Show"); // pressing will show next slide
    Button NextPicButton = new Button("Next Picture"); // pressing will show next slide
    Button BackPicButton = new Button("Prev Picture"); // pressing will show previous slide
    Label PicInfo = new Label("Click Start Button!", Label.CENTER); // text showing pic num
    static int PicNum = 0; // the id# of the picture
    static int MaxNumberofPics = 128; // the number of pics 
    static Image[] picArray = new Image[MaxNumberofPics]; //array holds all the pics
    static boolean firstShow = true; // the flag for the first showing (to show intro)
//*********************************************************************************************************************
// construct the grid layout
public void init()
{
    GridBagLayout myLayout = new GridBagLayout(); 
    GridBagConstraints myConstraints = new GridBagConstraints(); 
    setLayout(myLayout); 
    myConstraints.fill = GridBagConstraints.BOTH; 
    myConstraints.insets = new Insets(10,22,10,0); //(down, right, top&bottom, l&r)

    myConstraints.gridx = 0; myConstraints.gridy = 3; 
    myConstraints.gridwidth = 4; 
    myConstraints.gridheight = 1; myConstraints.weightx = 0.0; 
    myConstraints.weighty = 0.0; 
    myLayout.setConstraints(StartShowButton, myConstraints); 
    add(StartShowButton); 
    
    myConstraints.gridx = 3; myConstraints.gridy = 2; 
    myConstraints.gridwidth = 1; 
    myConstraints.gridheight = 1; myConstraints.weightx = 0.0; 
    myConstraints.weighty = 0.0; 
    myConstraints.insets = new Insets(240,22,0,0); //give vertical space to button 
    myLayout.setConstraints(NextPicButton, myConstraints); 
    add(NextPicButton); 
    
    myConstraints.gridx = 0; myConstraints.gridy = 2; 
    myConstraints.gridwidth = 1; 
    myConstraints.gridheight = 1; myConstraints.weightx = 0.0; 
    myConstraints.weighty = 0.0; 
    myLayout.setConstraints(BackPicButton, myConstraints); 
    add(BackPicButton); 
    
    setBackground(Color.black);//colors for label only
    setForeground(Color.white);

    myConstraints.gridx = 1; myConstraints.gridy = 2; 
    myConstraints.gridwidth = 2; 
    myConstraints.gridheight = 1; myConstraints.weightx = 0.0; 
    myConstraints.weighty = 0.0; 
    myLayout.setConstraints(PicInfo, myConstraints); 
    PicInfo.setFont(new Font("Times", Font.PLAIN, 12));
    add(PicInfo); 
    
    StartShowButton.addActionListener(this); 
    NextPicButton.addActionListener(this); 
    BackPicButton.addActionListener(this); 
    
    BackPicButton.setEnabled(false);
    NextPicButton.setEnabled(false);
    
    loadImages(); //begin downloading images
}
//************************************************************************************************************
// loads the images in the background
void loadImages()
{
    MediaTracker tracker = new MediaTracker(this);
    Image thePic = null;
    for (PicNum = 0; PicNum < MaxNumberofPics; PicNum++)
    {
        try
        { 
            //pictures must be called g0.jpg - gN.jpg
            thePic = getImage(getCodeBase(), "g" + PicNum + ".jpg"); 
            picArray[PicNum] = thePic;
        } 
        catch (Exception e)
        {
            System.out.println(e);
        }
        tracker.addImage(thePic, 0);
    } 
}
//************************************************************************************************************
public void paint (Graphics g)

    if (this.PicNum == MaxNumberofPics)
    {
        if (firstShow == true)
            this.PicNum = 0; //reset the pic number to the into caption for the first viewing
        else
            this.PicNum = 1; //reset the pic number to the first pic 
        BackPicButton.setEnabled(false);
    }
    else if (this.PicNum > 1)
    {
        BackPicButton.setEnabled(true);// enable the back button
        firstShow = false; // not the first showing so dont show the intro
    }
    else if (this.PicNum == 1)
    {
        BackPicButton.setEnabled(false);// enable the back button
        firstShow = false; // not the first showing so dont show the intro
    }
    setBackground(Color.black); //background color
    g.setColor(Color.white);
    g.fillRect(100,10,320,240);//picture filled with white to give slide projector effect
    g.drawRect(96,6,326,246);//draw border rectangle around pictures
    g.drawRect(96,258,326,60);//draw border rectangle around buttons

    if (this.PicNum >0)//show next pic number 
    {
        PicInfo.setText("Picture "+(this.PicNum)+ " of " +(MaxNumberofPics - 1));
    }
    g.drawImage(this.picArray[this.PicNum],100,10,this);//insert photo

//************************************************************************************************************
public void update (Graphics g) 
{
    paint(g);

//************************************************************************************************************
// check if button is clicked and update 

public void actionPerformed(ActionEvent aClick)
{
    Object feelClick = aClick.getSource(); 
    if (feelClick == NextPicButton)
    { //show next slide
        this.PicNum += 1; // the ID# of the next pic to be displayed
        this.repaint(); // method to show the image
    }
    else if (feelClick == BackPicButton)
    { //show last slide
        this.PicNum -= 1; // the ID# of the previous pic
        this.repaint(); // method to show the image
    }
    else if (feelClick == StartShowButton)
    { //start the slide show
        this.PicNum = 1; // goes to the first picture slide (set as g1 as g0 should be blank)
        this.repaint(); // method to show the image
        NextPicButton.setEnabled(true);// enable the next pic button
        StartShowButton.setLabel("Restart the Slideshow");
    }
}
//*********************************************************************************************************************
}
// end SlideShow.java