📄 photoframe.java
字号:
/* * * Copyright (c) 2007, Sun Microsystems, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package example.photoalbum;import java.util.Vector;import javax.microedition.lcdui.*;/** * This PhotoFrame provides the picture frame and drives the animation * of the frames and the picture. It handles the starting and stopping * of the Animation and contains the Thread used to do * the timing and requests that the Canvas be repainted * periodically. * It controls the border style and animation speed. */class PhotoFrame extends Canvas implements Runnable { /** * Mapping of speed values to delays in ms. * Indices map to those in the speed ChoiceGroup in the options form. * The indices are: 0 = stop, 1 = slow, 2 = medium, 3 = fast. * @see setSpeed */ private static final int[] speeds = { 999999999, 500, 250, 100, 0 }; /** border style */ private int style; /** animation speed set */ private int speed; /** Vector of images to display */ private Vector images; /** Index of next image to display */ private int index; /** X offset of image in frame */ private int imageX; /** X offset of image in frame */ private int imageY; /** Width and height of image */ private int imageWidth; /** Width and height of image */ private int imageHeight; /** Thread used for triggering repaints */ private Thread thread; /** buffer image of the screen */ private Image image; /** Pattern image used for border */ private Image bimage; /** Time of most recent paint */ private long paintTime; /** Time of most recent frame rate report */ private long statsTime; /** Number of frames since last frame rate report */ int frameCount; /** Last reported frame rate (for re-paint) */ int frameRate; /** * Create a new PhotoFrame. * Create an offscreen mutable image into which the border is drawn. * Border style is none (0). * Speed is stopped (0) until set. */ PhotoFrame() { image = Image.createImage(getWidth(), getHeight()); setStyle(0); setSpeed(0); } /** * Set the array of images to be displayed. * Update the width and height of the image and draw * the border to fit around it in the offscreen image. * @param images a vector of images to be displayed. */ void setImages(Vector images) { this.images = images; if (images.size() > 0) { Image image = (Image)images.elementAt(0); imageWidth = image.getWidth(); imageHeight = image.getHeight(); } else { imageWidth = 0; imageHeight = 0; } index = 0; imageX = (getWidth() - imageWidth) / 2; imageY = (getHeight() - imageHeight) / 2; genFrame(style, imageX, imageY, imageWidth, imageHeight); } /** * Advance to the next image and wrap around if necessary. */ void next() { if ((images == null) || (++index >= images.size())) { index = 0; } } /** * Back up to the previous image. * Wrap around to the end if at the beginning. */ void previous() { if ((images != null) && (--index < 0)) { index = images.size() - 1; } else { index = 0; } } /** * Reset the PhotoFrame so it holds minimal resources. * The animation thread is stopped. */ void reset() { images = null; thread = null; } /** * Handle key events. FIRE events toggle between * running and stopped. LEFT and RIGHT key events * when stopped show the previous or next image. * @param keyCode of the key pressed */ protected void keyPressed(int keyCode) { int action = getGameAction(keyCode); switch (action) { case RIGHT: if (thread == null) { next(); repaint(); } break; case LEFT: if (thread == null) { previous(); repaint(); } break; case FIRE: // Use FIRE to toggle the activity of the thread if (thread == null) { thread = new Thread(this); thread.start(); } else { synchronized (this) { // Wake up the thread to change the timing this.notify(); } // Shouldn't be in synchronized block thread = null; } break; } } /** * Handle key repeat events as regular key events. * @param keyCode of the key repeated */ protected void keyRepeated(int keyCode) { keyPressed(keyCode); } /** * Set the animation speed. * Speed: * <OL> * <LI>0 = stop * <LI>1 = slow * <LI>2 = medium * <LI>3 = fast * <LI>4 = unlimited * </OL> * @param speed speedo of animation 0-3; */ void setSpeed(int speed) { this.speed = speed; statsTime = 0; } /** * Get the speed at which animation occurs. * @return the current speed. * @see setSpeed */ int getSpeed() { return speed; } /** * Set the frame style. * Recreate the photo frame image from the current style * and location and size * <p> * Style: * <OL> * <LI> Style 0: No border is drawn. * <LI> Style 1: A simple border is drawn * <LI> Style 2: The border is outlined and an image * is created to tile within the border. * </OL> * @param style the style of the border; 0 = none, 1 = simple, * 2 = fancy. */ void setStyle(int style) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -