📄 animmanager.java
字号:
/* * AnimManager.java * * Copyright 2000 JJKING Software, Junichi Ito <jun1@mailhost.net> * Permission to use, copy, modify, and distribute this software and its * documentation without fee for NON-COMMERCIAL is free. */import java.util.Random;import com.sun.kjava.Graphics; /** * This class responsible for managing an animation. * All animation character, AnimChar instance, must be registered to this manager. * This manager perform animation as following step. * <ol> * <li>Call <tt>AnimChar.move()</tt> of all character to move them at one frame. * <li>Call <tt>AnimChar.paint()</tt> of all character to draw on screen. * <li>Go to 1. * </ol> * This will not manage the state of character. */public class AnimManager implements Runnable{ /** * The maximum number of animation character. */ static final int MAX_CHARNUM = 25; /** * The frame interval in milliseconds. */ static final int FRAME_INTERVAL = 250; /** * The field bounds of the animation. */ Rectangle bounds; /** * The characters managed by this. */ AnimChar[] animChars; /** * The listener which receives FrameEvent. */ FrameListener frameListener; /** * The random number generator. */ static final Random random = new Random(); /** * Create new manager with the specified rectangle. */ public AnimManager(Rectangle bounds) { this.bounds = bounds; // now the maximum number of manageable character is 1. animChars = new AnimChar[MAX_CHARNUM]; Thread worker = new Thread(this); worker.start(); } /** * Animation loop. */ public void run() { Graphics g = Graphics.getGraphics(); int cnt = 0; FrameEvent evt = new FrameEvent(cnt); while(true) { // to protect characters list in performing animation // by other thread. synchronized(this) { for (int i = 0; i < animChars.length; i++) { if (animChars[i] != null) { animChars[i].move(); } } for (int i = 0; i < animChars.length; i++) { if (animChars[i] != null) { animChars[i].paint(g); } } for (int i = 0; i < animChars.length; i++) { if (animChars[i] != null && animChars[i].getState() == AnimChar.STATE_HIDE) { remove(animChars[i]); } } // invoke frame listener if (frameListener != null) { evt.setCount(cnt); frameListener.changeFrame(evt); } } // frame interval try { Thread.sleep(FRAME_INTERVAL); } catch(InterruptedException ex) {} cnt ++; } } /** * Update the specified character immediately. * Application may called this method when the character state changed in interactive. * @param c the character to be updated. */ public synchronized void update(AnimChar c) { Graphics g = Graphics.getGraphics(); c.move(); c.paint(g); if (c.getState() == AnimChar.STATE_HIDE) remove(c); } /** * Add the specified character to be managed. * @param c the character to be added. * @param AnimException thrown when fail to add the given character. */ public synchronized void add(AnimChar c) throws AnimException { for (int i = 0; i < animChars.length; i++) { if (animChars[i] == null) { animChars[i] = c; animChars[i].start(this); return; } } throw new AnimException("Can't add any more"); } /** * Returns all characters managed by this. */ public AnimChar[] getAnimChars() { return animChars; } /** * Remove the specified character to be stop to manage. * @param c the character to be removed. */ public synchronized void remove(AnimChar c) { for (int i = 0; i < animChars.length; i++) { if (animChars[i] == c) { animChars[i] = null; } } } /** * Remove all characters from the manager. */ public synchronized void reset() { for (int i = 0; i < animChars.length; i++) { animChars[i] = null; } } /** * Returns the bounds of animation field. */ public Rectangle getFieldBounds() { return bounds; } /** * This is the utility method to get random number between 0 * and the specified number. * @param the maximum number. */ public static int random(int max) { int r = random.nextInt(); return (r < 0 ? -r : r) % max; } /** * Set the frame listener. * If null is given, clear the listsner. * @param listsner the listener. */ public void setFrameListener(FrameListener listener) { frameListener = listener; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -