/** * Class that represents a slide show. A slide show has * an array of pictures, a time to wait between pictures, * and a title that is shown at the beginning of the show. * * @author Barb Ericson */ public class SlideShow { ///////////// fields ////////////// /** title for this slide show */ private String title; /** the time to wait between pictures (in milliseconds) */ private int waitTime = 2000; // default of 2 seconds /** the array of pictures to show */ private Picture[] pictureArray; /////////////// constructors ///////// /** * A no-argument constructor that takes * no parameters and leaves fields with their * default values */ public SlideShow() {} /** * A constructor that takes just the title * @param theTitle the title to use */ public SlideShow(String theTitle) { this.title = theTitle; } /** * A constructor that takes * the array of pictures * @param pictArray the array of pictures */ public SlideShow(Picture[] pictArray) { this.pictureArray = pictArray; } /** * A constructor that takes the title and * the array of pictures * @param theTitle the title to use * @param pictArray the array of pictures */ public SlideShow(String theTitle, Picture[] pictArray) { this.title = theTitle; this.pictureArray = pictArray; } /** * A constructor that takes * the array of pictures, the wait * time, and the title * @param theTitle the title to use * @param pictArray the array of pictures */ public SlideShow(Picture[] pictArray, int theWaitTime, String theTitle) { this.title = theTitle; this.waitTime = theWaitTime; this.pictureArray = pictArray; } /////////////// methods ///////////// /** * Method to return the title * @return the title or null if none */ public String getTitle() { return this.title; } /** * Method to return the picture at the * passed index * @param index the index for the picture * @return the picture at the index */ public Picture getPicture(int index) { return this.pictureArray[index]; } /** * Method to get the time to wait * between showing pictures * @return the time to wait in milliseconds */ public int getWaitTime() { return this.waitTime; } /** * Method to return the number of pictures * in this slide show * @return the number of pictures in the show */ public int getNumPictures() { if (pictureArray == null) return 0; else return pictureArray.length; } /** * Method to set a picture at the passed * index * @param index which one to change * @param thePict the picture to use * @return true if success else return false */ public boolean setPicture(int index, Picture thePict) { if (thePict != null && pictureArray != null) { pictureArray[index] = thePict; return true; } else return false; } /** * Method to set the array of pictures * @param theArray the array to use * @return true if success else false */ public boolean setPictureArray(Picture[] theArray) { if (pictureArray == null) { this.pictureArray = theArray; return true; } else { return false; } } /** * Method to set the title for this show * @param theTitle the title to use */ public void setTitle(String theTitle) { this.title = theTitle; } /** * Method to return a string with information * about this object * @return a string with the object information */ public String toString() { return "Slide show with title: " + this.title + ", wait time of: " + this.waitTime + " in milliseconds " + "and " + getNumPictures() + " pictures in it."; } /** * Method to show the slide show * @throws Exception */ public void show() throws Exception { // check that the picture array isn't null if (pictureArray != null) { // if the title isn't null show it if (title != null) { Picture titleP = new Picture(640,480); titleP.drawHorizontalCenteredString(title,240); titleP.show(); Thread.sleep(this.waitTime); titleP.hide(); } /** Loop through the pictures */ for (Picture currPict : this.pictureArray) { currPict.show(); Thread.sleep(waitTime); currPict.hide(); } } } /** * Method to check if this slide show contains the passed * picture * @param p the picture to look for * @return true if found else false */ public boolean contains(Picture p) { return Searcher.linearFind(p,this.pictureArray); } public static void main(String[] args) throws Exception { Picture p1 = new Picture(FileChooser.getMediaPath("beach.jpg")); Picture p2 = new Picture(FileChooser.getMediaPath("church.jpg")); Picture p3 = new Picture(FileChooser.getMediaPath("greece.jpg")); Picture[] pictArray = {p1,p2,p3}; SlideShow show1 = new SlideShow("Vacation Pictures",pictArray); show1.show(); System.out.println(show1); System.out.println(show1.contains(p1)); Picture p4 = new Picture(FileChooser.getMediaPath("barbara.jpg")); System.out.println(show1.contains(p4)); } }