📄 animation.java
字号:
/* * @(#)Animation.java 1.5 01/04/04 * * Copyright (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package examples.photoalbum;import java.io.IOException;import java.util.Vector;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.Graphics;/** * An Animation contains the set of images to display. * Images are read from resource files supplied in the * JAR file. * <p> * This implementation keeps the Images in the heap. * If memory is short, a more deliberate management * of Image may be required. */class Animation { /** * Location to draw the animation, set these fields to * change the location where the image is drawn. */ int x, y; /** * The width and the height of the images (max of all if they * are different). * They are set when images are loaded and should not be changed. */ int width, height; /** * Vector of images in the sequence. */ private Vector images; /** * Current index into the sequence of images. */ private int index; /** * Size of sequence of images. * Set to a large number until the last image of * the sequence has been read. */ private int size; /** * Prefix or name of the image. */ private String prefix; /** * Create a new Animation. */ Animation() { images = new Vector(30); } /** * Advance to the next image. * If the number of images is known then just advance * and wrap around if necessary. * If the number of images is not known then when * advancing off the end of the known images try to * create a new image using the pattern. * When an attempt fails that sets the number of images. */ void next() { int nextindex = index + 1; if (nextindex >= size) { index = 0; } else if (nextindex >= images.size()) { // Try to read the next image // If that works put it into the images vector try { String name = prefix + nextindex + ".png"; Image image = Image.createImage(name); images.setSize(nextindex+1); images.setElementAt(image, nextindex); index = nextindex; } catch (IOException ex) { // No more images, set the size of the sequence. size = nextindex; index = 0; } catch (Exception e) { size = nextindex; index = 0; } } else { // Index is within range of Images already read index = nextindex; } } /** * Back up to the previous image. * Wrap around to the end if at the beginning. */ void previous() { index--; if (index < 0) { index = images.size()-1; } } /** * Paint the current image in the sequence. * The image is drawn to the target graphics context * at the x, and y of the Animation. * @param g graphics context to which the next image is drawn. */ public void paint(Graphics g) { if (images.size() > 0) { g.drawImage((Image)images.elementAt(index), x, y, 0); } } /** * Load Images from resource files using * <code>Image.createImage</code>. * The first image is loaded to determine whether it is a * single image or a sequence of images and to make sure it exists. * Subsequent images are loaded on demand when they are needed. * If the name given is the complete name of the image then * it is a singleton. * Otherwise it is assumed to be a sequence of images * with the name as a prefix. Sequence numbers (n) are * 0, 1, 2, 3, .... The full resource name is the concatenation * of name + n + ".png". * <p> * Subsequent images are loaded when they are needed. See * <code>next</code> and <code>previous</code> for details. * @param name the name or prefix of the resource image names * @exception IOException is thrown if the image or the first * of the sequence cannot be found. * @exception OutOfMemoryError if no memory can be allocated for * the image. */ void loadImage(String prefix) throws IOException { this.prefix = prefix; Image image = null; images.setSize(0); index = 0; try { // Try the name supplied for the single image case. // If it is found then do the setup and return image = Image.createImage(prefix); size = 1; } catch (IOException ex) { // Use the prefix + "0.png" to locate the first of // a series of images. String name = prefix + "0.png"; image = Image.createImage(name); size = 999999999; } width = image.getWidth(); height = image.getHeight(); images.addElement(image); } /** * Reset the Animation to reduce memory usage. * Discard all but the first image. */ void reset() { if (images.size() > 0) { for (int i = 0; i < images.size(); i++) images.setElementAt(null, i); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -