⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animmanager.java

📁 j2me 的小游戏
💻 JAVA
字号:
/*
 * AnimManager.java
 *
 * Copyright 1999 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.
 *
 * @version 1.0  1999/10/2
 */
public class AnimManager implements Runnable
{
	/**
	 * The field bounds of the animation.
	 */
	Rectangle bounds;

	/**
	 * The characters managed by this.
	 */
	AnimChar[] animChars;

	/**
	 * The random number generator.
	 */
	static 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[1];
		
		Thread worker = new Thread(this);
		worker.start();
	}
	
	/**
	 * Animation loop. 
	 */
	public void run()
	{
		Graphics g = Graphics.getGraphics();
		while(true) {
			// to protect characters list in performing animation
			// by other thread.
			synchronized(animChars) {
				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]);
					}
				}
			}
			try { Thread.sleep(300); }
			catch(InterruptedException ex) {
				break;
			}
		}
	}
	
	/**
	 * 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 void add(AnimChar c) throws AnimException
	{
		// to protect characters list in adding.
		synchronized(animChars) {
			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");
		}
	}
	
	/**
	 * Remove the specified character to be stop to manage.
	 * @param c the character to be removed.
	 */
	public synchronized void remove(AnimChar c)
	{
		// to protect characters list in removing.
		synchronized(animChars) {
			for (int i = 0; i < animChars.length; i++) {
				if (animChars[i] == c) { 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;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -