import java.awt.*; import objectdraw.*; /** * Program will manage a collection of digital photographs for the user. At * start up, photos will be displayed as thumbnails. User may select the desired * thumbnail to view the full-size photo within the same display window. The * PhotoViewer class creates the initial display and processes mouse/event * handling methods. */ public class PhotoViewer extends FrameWindowController { /** * Where the top of the bottom row should be */ private static final int THUMBNAIL_BOTTOM_ROW_TOP = 600; /** * Where the left edge of the first thumbnail goes. */ private static final int THUMBNAIL_FIRST_COL_LEFT = 10; /** * The size of the font in the title bar */ private static final int TEXT_FONT_SIZE = 20; /** * The height of the title bar */ private static final int BAR_HEIGHT = 30; /** * How far the title bar should be from the right edge of the window */ private static final int BAR_RIGHT_MARGIN = 40; /** * How far the title bar should be from the top edge of the window */ private static final int BAR_TOP_MARGIN = 48; /** * How far the title bar should be from the left edge of the window */ private static final int BAR_LEFT_MARGIN = 5; /** * How far the title text should be from the top edge of the title bar */ private static final int TEXT_TOP_MARGIN = 2; /** * How far the title text should be from the top edge of the window */ private static final int TEXT_TOP = BAR_TOP_MARGIN + TEXT_TOP_MARGIN; /** * How far the title text should be from the left edge of the title bar */ private static final int TEXT_LEFT_MARGIN = 5; /** * How far the title text should be from the left edge of the window */ private static final int TEXT_LEFT = BAR_LEFT_MARGIN + TEXT_LEFT_MARGIN; /** * The amount of space from the left edge of one thumbnail to the left edge * of the next thumbnail. */ private static final int HORIZONTAL_SPACING = 100; /** * The amount of space from the top edge of one thumbnail to the top edge * of the next thumbnail. */ private static final int VERTICAL_SPACING = 100; /** * background color of display window. */ private static final Color BACKGROUND_COLOR = Color.BLUE; /** * Background color when the mouse is not in the window. */ private static final Color DARK_BACKGROUND = BACKGROUND_COLOR.darker(); /** * Color of the title bar. */ private static final Color BACKBAR_COLOR = Color.WHITE; /** * Color of the title bar when the mouse is not in the window. */ private static final Color DARK_BACKBAR = BACKBAR_COLOR.darker(); /** * Size of the window. */ private static final int GRID_SIZE = 800; /** * Number of columns of thumbnails */ private static final int NUM_COLS = 4; /** * Number of thumbnails */ private static final int NUM_PICS = 25; /** * Number of rows of thumbnails */ private static final int NUM_ROWS = NUM_PICS / NUM_COLS; /** * The background rectangle */ private FilledRect background; /** * The title bar */ private FilledRect backgroundBar; /** * The picture being displayed at full size */ private Thumbnail bigPic; /** * All the thumbnails. */ private ThumbnailCollection allThumbnails = new ThumbnailCollection(); /** * When the program starts, draw the background and title bar. Display the * thumbnails in a grid. * * @see objectdraw.WindowController#begin() */ public void begin() { // Resizing window to fit thumbnails. resize(GRID_SIZE, GRID_SIZE); // Draw the background and title bar background = new FilledRect(0, 0, GRID_SIZE, GRID_SIZE, canvas); background.setColor(BACKGROUND_COLOR); backgroundBar = new FilledRect(BAR_LEFT_MARGIN, BAR_TOP_MARGIN, GRID_SIZE - BAR_RIGHT_MARGIN, BAR_HEIGHT, canvas); backgroundBar.setColor(BACKBAR_COLOR); Text text = new Text("PHOTO VIEWER", TEXT_LEFT, TEXT_TOP, canvas); text.setFontSize(TEXT_FONT_SIZE); text.setBold(); // Draw the grid of thumbnails drawThumbnailGrid(); } /** * Draws the thumbnails in a grid */ private void drawThumbnailGrid() { Thumbnail picture; double start_X = THUMBNAIL_FIRST_COL_LEFT; double start_Y = THUMBNAIL_BOTTOM_ROW_TOP; int whichPic = 0; // Draw all but the last row. for (int row = 0; row < NUM_ROWS - 1; row++) { for (int col = 0; col < NUM_COLS; col++) { addThumbnail(start_X, start_Y, whichPic); start_X = start_X + HORIZONTAL_SPACING; whichPic++; } start_X = 0; start_Y = start_Y - VERTICAL_SPACING; } // The last row has more thumbnails. while (whichPic < NUM_PICS) { addThumbnail(start_X, start_Y, whichPic); start_X = start_X + HORIZONTAL_SPACING; whichPic++; } } /** * Add a thumbnail to the grid * @param start_X the left edge of the thumbnail * @param start_Y the top edge of the thumbnail * @param whichPic the thumbnail index */ private void addThumbnail(double start_X, double start_Y, int whichPic) { Thumbnail picture; String p = "pic" + whichPic + ".jpg"; picture = new Thumbnail(p, start_X, start_Y, canvas); allThumbnails.addPic(picture); } /** * Restore the original colors when the mouse enters the window * @param point where the mouse is * @see objectdraw.WindowController#onMouseEnter(objectdraw.Location) */ public void onMouseEnter(Location point) { background.setColor(BACKGROUND_COLOR); backgroundBar.setColor(BACKBAR_COLOR); } /** * Make the window darker when the mouse leaves the window * @param point where the mouse is * @see objectdraw.WindowController#onMouseExit(objectdraw.Location) */ public void onMouseExit(Location point) { background.setColor(DARK_BACKGROUND); backgroundBar.setColor(DARK_BACKBAR); } /** * Display a fullsize version of the thumbnail the user depresses the mouse button on. * If the user presses on the background. Do nothing. * @param point where the user depressed the mouse button. * @see objectdraw.WindowController#onMousePress(objectdraw.Location) */ public void onMousePress(Location point) { Thumbnail selectedPic = allThumbnails.getPicAt(point); if (selectedPic != null) { if (bigPic != null) { bigPic.hidePic(); } bigPic = selectedPic; bigPic.showPic(); } } }