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

📄 animation.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// Copyright 2002 Nokia Corporation. 
// 
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER, 
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS 
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE 
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE 
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO 
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR 
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE 
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT 
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED 
// BY THIRD PARTIES 
// 
// Furthermore, information provided in this source code is preliminary, 
// and may be changed substantially prior to final release. Nokia Corporation 
// retains the right to make changes to this source code at 
// any time, without notice. This source code is provided for informational 
// purposes only. 
// 
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
// 
// A non-exclusive, non-transferable, worldwide, limited license is hereby 
// granted to the Licensee to download, print, reproduce and modify the 
// source code. The licensee has the right to market, sell, distribute and 
// make available the source code in original or modified form only when 
// incorporated into the programs developed by the Licensee. No other 
// license, express or implied, by estoppel or otherwise, to any other 
// intellectual property rights is granted herein.


package example.postcard;

import java.lang.IllegalArgumentException;
import java.util.Random;
import javax.microedition.lcdui.*;


// A class representing the animated image that floats in the Postcard

class Animation
{
    private static final Random RANDOM = new Random();

    private final AnimationSequence animationSequence;
    private final int w;
    private final int h;
    private final int xBoundary;
    private final int yBoundary;
    private final int wBoundary;
    private final int hBoundary;

    private int currentIndex = 0;
    private int ticks = 0;
    private int ticksSinceLastChangeX = 0;
    private int ticksSinceLastChangeY = 0;
    private int x = 0;
    private int y = 0;
    private int dx = 1;
    private int dy = 1;


    // An Animation is constructed from a sequence of animation images.
    // The Animation can move with a box bounded by {xBoundary, yBoundary}
    // and {xBoundary+wBoundary, yBoundary+hBoundary}.
    //
    // The parameter animationSequence is the sequence of animation
    // images used to create the animation. The bounding box where the
    // animation is allowed to move is bounded by: xBoundary, yBoundary,
    // wBoundary and hBoundary.

    Animation(AnimationSequence animationSequence,
                    int xBoundary, int yBoundary, int wBoundary, int hBoundary)
        throws IllegalArgumentException
    {
        if (animationSequence == null)
        {
            throw new IllegalArgumentException("null AnimationSequence");
        }

        this.animationSequence = animationSequence;
        this.w = animationSequence.getMaxImageWidth();
        this.h = animationSequence.getMaxImageHeight();
        if (w < wBoundary)
        {
            x = (xBoundary + wBoundary - w)/2;
        }
        if (h < hBoundary)
        {
            y = (yBoundary + hBoundary - h)/2;
        }
        this.xBoundary = xBoundary;
        this.yBoundary = yBoundary;
        this.wBoundary = wBoundary;
        this.hBoundary = hBoundary;
        dx = rand(3) - 1; // 0, -1 or 1
        dy = rand(3) - 1; // 0, -1 or 1
    }


    void tick()
    {
        ticks++;
        ticksSinceLastChangeX++;
        ticksSinceLastChangeY++;

        if ((ticks % 5) == 0)
        {
            if ((++currentIndex) >= animationSequence.size())
            {
                currentIndex = 0;
            }
        }

        move();
    }


    private void move()
    {
        // mostly continue as we are, but sometimes randomly change
        if ((ticksSinceLastChangeX > 20) && (rand(20) == 0))
        {
            dx = rand(3) - 1; // 0, -1 or 1
            ticksSinceLastChangeX = 0;
        }

        // if moving would take us off-canvas left or right, reverse
        if (((x + dx) < xBoundary) || ((x + dx + w) > (xBoundary + wBoundary)))
        {
            dx = -dx;
            ticksSinceLastChangeX = 0;
        }


        // mostly continue as we are, but sometimes randomly change
        if ((ticksSinceLastChangeY > 20) && (rand(20) == 0))
        {
            dy = rand(2) * 2 - 1; // -1 or 1
            ticksSinceLastChangeY = 0;
        }

        // if moving would take us off-canvas top or bottom, reverse
        if (((y + dy) < yBoundary) || ((y + dy + h) > (yBoundary + hBoundary)))
        {
            dy = -dy;
            ticksSinceLastChangeY = 0;
        }

        x += dx;
        y += dy;
    }


    void draw(Graphics g)
    {
        Image image = animationSequence.elementAt(currentIndex);
        g.drawImage(image, x, y, (Graphics.LEFT | Graphics.TOP));
    }


    // This method does not generate true random numbers.

    static int rand(int scale)
    {
        return (RANDOM.nextInt() << 1 >>> 1) % scale;
    }
}

⌨️ 快捷键说明

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