home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-09-28 | 13.8 KB | 539 lines | [TEXT/CWIE] |
- import java.awt.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.KeyEvent;
- import java.io.File;
- import java.util.Vector;
-
- import com.apple.mrj.MRJApplicationUtils;
- import com.apple.mrj.MRJOpenDocumentHandler;
- import com.apple.mrj.MRJQuitHandler;
- import com.apple.mrj.MRJAboutHandler;
-
- public class SlideShow extends Frame
- {
- //Declare and define constants
- //Insert "SlideShow Constants"
- protected static final int SLEEP_DELAY = 1000;
- protected static final int WIDTH = 430;
- protected static final int HEIGHT = 270;
-
- //Declare data members
- //Insert "SlideShow data members"
- protected Vector files;
- protected Image currentImage;
- protected int currentIndex;
- protected boolean isFullScreen;
- protected boolean isPlaying;
- protected PlayRunnable playRunnable;
- protected Thread thread;
- protected FileDialog openFileDialog1;
- protected Controller controls;
- protected AboutDialog aboutDialog1;
-
- //DECLARE_MENUS
- //Declare Menus, Menu Items and the Menu Bar
- //Insert "SlideShow declare menus"
- protected MenuBar menuBar1;
- protected Menu fileMenu;
- protected MenuItem openMenuItem;
- protected MenuItem quitMenuItem;
- protected Menu slideShowMenu;
- protected MenuItem playMenuItem;
- protected MenuItem backMenuItem;
- protected MenuItem forwardMenuItem;
- protected Menu optionsMenu;
- protected MenuItem controlsMenuItem;
- protected MenuItem fullScreenMenuItem;
-
- /**
- * The entry point to our application
- */
- static public void main(String args[])
- {
- //Instantiate our SlideShow, make it visible, and register our MRJ handlers.
- //Insert "SlideShow main"
- SlideShow slideShow = new SlideShow();
- slideShow.setVisible(true);
- slideShow.registerMRJHandlers();
- }
-
- public SlideShow()
- {
- //INIT_STATE
- //Initialize state information
- //Insert "SlideShow init state"
- isFullScreen = false;
- isPlaying = false;
- files = new Vector();
- currentImage = null;
- currentIndex = -1;
-
- //INIT_CONTROLS
- //Setup and configure our SlideShow
- //Insert "SlideShow init controls"
- setLayout(null);
- setVisible(false);
- setSize(WIDTH, HEIGHT);
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- setLocation((screenSize.width - WIDTH) / 2, (screenSize.height - HEIGHT) / 2);
- setBackground(Color.black);
- openFileDialog1 = new FileDialog(this);
- openFileDialog1.setMode(FileDialog.LOAD);
- openFileDialog1.setTitle("Open");
- openFileDialog1.setFilenameFilter(new ImageNameFilter());
- setTitle("SlideShow");
- controls = new Controller(this);
-
- //INIT_MENUS
- //Create, configure, and setup the menubar, menus, and menu items.
- //Insert "SlideShow init menus"
- menuBar1 = new MenuBar();
-
- //File menu
- fileMenu = new Menu("File");
- openMenuItem = new MenuItem("Open...");
- openMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_O,false));
- fileMenu.add(openMenuItem);
- fileMenu.addSeparator();
- quitMenuItem = new MenuItem("Quit");
- quitMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_Q,false));
- fileMenu.add(quitMenuItem);
- menuBar1.add(fileMenu);
-
- //SlideShow menu
- slideShowMenu = new Menu("SlideShow");
- playMenuItem = new MenuItem("Toggle Play");
- playMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_P,false));
- slideShowMenu.add(playMenuItem);
- backMenuItem = new MenuItem("Back");
- backMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_OPEN_BRACKET,false));
- slideShowMenu.add(backMenuItem);
- forwardMenuItem = new MenuItem("Forward");
- forwardMenuItem.setShortcut(new MenuShortcut(KeyEvent.VK_CLOSE_BRACKET,false));
- slideShowMenu.add(forwardMenuItem);
- menuBar1.add(slideShowMenu);
-
- //Options menu
- optionsMenu = new Menu("Options");
- controlsMenuItem = new MenuItem("Toggle Controls");
- optionsMenu.add(controlsMenuItem);
- fullScreenMenuItem = new MenuItem("Toggle Full Screen");
- optionsMenu.add(fullScreenMenuItem);
- menuBar1.add(optionsMenu);
- setMenuBar(menuBar1);
-
- //REGISTER_LISTENERS
- //Register ActionListeners with the menu items and the controller.
- //Insert "SlideShow register listeners"
- Action aAction = new Action();
- openMenuItem.addActionListener(aAction);
- quitMenuItem.addActionListener(aAction);
- controlsMenuItem.addActionListener(aAction);
- fullScreenMenuItem.addActionListener(aAction);
- playMenuItem.addActionListener(aAction);
- backMenuItem.addActionListener(aAction);
- forwardMenuItem.addActionListener(aAction);
- controls.addActionListener(aAction);
- }
-
- /**
- * Starts or stops cycling forward through the list of image files to display.
- */
- public void togglePlaying()
- {
- //Handle starting and stopping the automatic progression of the show.
- //Insert "SlideShow togglePlaying"
- if (isPlaying)
- {
- if (playRunnable != null)
- playRunnable.isRun = false;
- isPlaying = false;
- }
- else
- {
- if (thread == null || !thread.isAlive())
- {
- if (playRunnable != null)
- playRunnable.isRun = false;
-
- playRunnable = new PlayRunnable();
- thread = new Thread(playRunnable);
- thread.start();
- isPlaying = true;
- }
- }
- }
-
- //Inner class to implement our automatic progression of the show.
- //Insert "SlideShow PlayRunnable"
- class PlayRunnable implements Runnable
- {
- public boolean isRun = true;
-
- public void run()
- {
- while (isRun)
- {
- oneStep(true);
-
- try
- {
- Thread.sleep(SLEEP_DELAY);
- }
- catch (InterruptedException exc) { }
- }
- }
- }
-
- /**
- * Steps the slide show forward or backwards by one image.
- * @param if true, step forward, if false, step backward.
- */
- public void oneStep(boolean isForward)
- {
- //Handle stepping forward or backward in the list of image files,
- //load the image, and repainting.
- //Insert "SlideShow oneStep"
- int size = files.size();
-
- if (size > 0)
- {
- if (isForward)
- {
- currentIndex++;
- if (currentIndex >= size)
- currentIndex = 0;
- }
- else
- {
- currentIndex--;
- if (currentIndex < 0)
- currentIndex = size - 1;
- }
-
- File file = (File)files.elementAt(currentIndex);
- if (file != null)
- {
- Image image = Misc.loadImage(file.getPath(), this, false);
- if (image != null)
- {
- if (currentImage != null)
- currentImage.flush();
- currentImage = image;
- repaint();
- }
- }
- }
- }
-
- /**
- * Handles sizing of the window to utilize the full screen size, or to use the size of the image.
- */
- public void toggleFullScreen()
- {
- //Handle toggling the frame window size between the image size, screen size, and full screen.
- //Insert "SlideShow toggleFullScreen"
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-
- if (isFullScreen)
- {
- int width = WIDTH;
- int height = HEIGHT;
-
- if (currentImage != null)
- {
- width = currentImage.getWidth(this);
- height = currentImage.getHeight(this);
-
- //Make sure the window fits on the screen
- width = Math.min(width, screenSize.width);
- height = Math.min(height, screenSize.height);
- }
-
- setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
- setSize(width, height);
-
- isFullScreen = false;
- }
- else
- {
- int top = 21;
- int sides = 5;
- setBounds(-sides, -top, screenSize.width + 2 * sides, screenSize.height + top + sides);
- isFullScreen = true;
- }
- }
-
- /**
- * Shows or hides the control window.
- */
- public void toggleControls()
- {
- //Handle toggling the visibility of the controller
- //Insert "SlideShow toggleControls"
- if (controls != null)
- controls.setVisible(!controls.isVisible());
- }
-
- /**
- * Gets called when the user chooses the Quit menu item, or when the
- * application receives a quit message from the Finder (or other app).
- */
- protected void doOnQuit()
- {
- //Handle cleaning up, and quit.
- //Insert "SlideShow doOnQuit"
- //Do any clean up here.
-
- //Exit with success.
- System.exit(0);
- }
-
- /**
- * Gets called when the user selects the about menu item in the Apple Menu.
- */
- protected void doAbout()
- {
- //Handle displaying about information here
- //Insert "SlideShow doAbout"
- if (aboutDialog1 == null)
- aboutDialog1 = new AboutDialog(this, true);
- aboutDialog1.setVisible(true);
- }
-
- public void paint(Graphics g)
- {
- //Handle scaling and drawing the image to fit in the frame content area.
- //Insert "SlideShow paint"
- if (currentImage != null)
- {
- Dimension s = getSize();
- int iWidth = currentImage.getWidth(this);
- int iHeight = currentImage.getHeight(this);
-
- int scaleWidth = iWidth;
- int scaleHeight = iHeight;
-
- int wDelta = s.width - iWidth;
- int hDelta = s.height - iHeight;
-
- if (wDelta > 0 && hDelta > 0)
- {
- //The image fits, just draw it.
- g.drawImage(currentImage, (s.width - iWidth) / 2, (s.height - iHeight) / 2, this);
- }
- else
- {
- //The image doesn't fit. We need to scale it down to fit.
-
- float ratio = 1;
-
- if (wDelta < hDelta)
- {
- if (iWidth > 0)
- {
- //width needs to be scaled to fit
- ratio = s.width / (float)iWidth;
- }
- }
- else
- {
- if (iHeight > 0)
- {
- //height needs to be scaled to fit
- ratio = s.height / (float)iHeight;
- }
- }
-
- scaleWidth = (int)(iWidth * ratio);
- scaleHeight = (int)(iHeight * ratio);
-
- g.drawImage(currentImage, (s.width - scaleWidth) / 2, (s.height - scaleHeight) / 2, scaleWidth, scaleHeight, this);
- }
- }
- }
-
- public void setVisible(boolean b)
- {
- //Make sure the controls are visible only when the frame is visible.
- //Insert "SlideShow setVisible"
- super.setVisible(b);
-
- if (controls != null)
- controls.setVisible(b);
- }
-
-
- protected void registerMRJHandlers()
- {
- //Register MRJ handlers for open, about and quit.
- //Insert "SlideShow registerMRJHandlers"
- MRJI IMRJI = new MRJI();
- MRJApplicationUtils.registerOpenDocumentHandler(IMRJI);
- MRJApplicationUtils.registerQuitHandler(IMRJI);
- MRJApplicationUtils.registerAboutHandler(IMRJI);
- }
-
- //Inner class defining the MRJ Interface
- //Insert "SlideShow MRJI"
- class MRJI implements MRJOpenDocumentHandler, MRJQuitHandler, MRJAboutHandler
- {
-
- /**
- * This gets called by MRJ for each file/folder dropped onto the application.
- * If the file is a directory, this recurses through the directory structure
- * collecting image files.
- * @param the file to add to the list of image files to display, or the File
- * object to recurse to look for image files to add to the list.
- */
- public void handleOpenFile(File file)
- {
- if(file != null)
- {
- if (file.isDirectory())
- {
- String directory = file.getPath();
- if (!directory.endsWith("/"))
- directory += "/";
-
- String[] fileList = file.list();
-
- for (int fileInd = 0; fileInd < fileList.length; fileInd++)
- this.handleOpenFile(new File(directory + fileList[fileInd]));
- }
- else
- {
- String upperCaseName = file.getName().toUpperCase();
- if (upperCaseName.endsWith(".JPG") ||
- upperCaseName.endsWith(".JPEG") ||
- upperCaseName.endsWith(".GIF"))
- {
- files.addElement(file);
- }
- }
- }
- }
-
- /**
- * This gets called when the application receives a quit event.
- */
- public void handleQuit()
- {
- doOnQuit();
- }
-
- /**
- * This gets called when the About menu item in the Apple Menu is selected.
- */
- public void handleAbout()
- {
- doAbout();
- }
- }
-
- //Inner class for handling ActionEvents
- //Insert "SlideShow Action"
- class Action implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- Object object = event.getSource();
- if (object == openMenuItem)
- openMenuItem_ActionPerformed(event);
- else if (object == quitMenuItem)
- quitMenuItem_ActionEvent(event);
- else if (object == controlsMenuItem)
- controlsMenuItem_ActionPerformed(event);
- else if (object == fullScreenMenuItem)
- fullScreenMenuItem_ActionPerformed(event);
- else if (object == playMenuItem)
- playMenuItem_ActionPerformed(event);
- else if (object == backMenuItem)
- backMenuItem_ActionPerformed(event);
- else if (object == forwardMenuItem)
- forwardMenuItem_ActionPerformed(event);
- else if (object == controls)
- controls_ActionPerformed(event);
- }
- }
-
- //Routines for handling the various ActionEvents
- //Insert "SlideShow Action Management"
- void openMenuItem_ActionPerformed(ActionEvent event)
- {
- //Present the load file dialog.
- openFileDialog1.setVisible(true);
-
- //Make sure a valid value is returned (user could cancel).
- String resultPath = openFileDialog1.getDirectory();
- String resultFile = openFileDialog1.getFile();
- if(resultPath != null && resultPath.length() != 0 && resultFile != null && resultFile.length() != 0)
- {
- //Construct a File object from the information from the dialog.
- File file = new File(resultPath + resultFile);
- if(file != null)
- {
- //Add the file to our list of image files.
- files.addElement(file);
-
- //Load the image from the file, and display it as our current image.
- Image image = Misc.loadImage(file.getPath(), this, false);
- if (image != null)
- {
- if (currentImage != null)
- currentImage.flush();
- currentImage = image;
- currentIndex = files.size() - 1;
- repaint();
- }
- }
- }
- }
-
- void quitMenuItem_ActionEvent(ActionEvent event)
- {
- doOnQuit();
- }
-
- void controlsMenuItem_ActionPerformed(ActionEvent event)
- {
- toggleControls();
- }
-
- void fullScreenMenuItem_ActionPerformed(ActionEvent event)
- {
- toggleFullScreen();
- }
-
- void playMenuItem_ActionPerformed(ActionEvent event)
- {
- togglePlaying();
- controls.setPlayState(!isPlaying);
- }
-
- void backMenuItem_ActionPerformed(ActionEvent event)
- {
- oneStep(false);
- }
-
- void forwardMenuItem_ActionPerformed(ActionEvent event)
- {
- oneStep(true);
- }
-
- void controls_ActionPerformed(ActionEvent event)
- {
- String command = event.getActionCommand();
-
- if (command.equals(Controller.BACKWARD_COMMAND))
- oneStep(false);
- else if (command.equals(Controller.FORWARD_COMMAND))
- oneStep(true);
- else if (command.equals(Controller.PLAY_COMMAND))
- togglePlaying();
- else if (command.equals(Controller.PAUSE_COMMAND))
- togglePlaying();
- }
- }
-