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

📄 fireworkscanvas.java

📁 J2ME核心类及MIDlet类 MIDP用户界面对象 图形处理及低级事件处理 多线程编程 I/O及网络编程 数据库RMS编程 浮点数编程 多媒体及GAME API编程 安全、加密及
💻 JAVA
字号:
//
// Copyright 2002 Nokia Mobile Phones Ltd.
//
// 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 Mobile
// Phones Ltd. retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Third-party brands and names are the property of their respective
// owners. Java(TM) and J2ME(TM) are registered trademarks of
// Sun Microsystems Inc.
//
// 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.fireworks;

import javax.microedition.lcdui.*;
import java.util.Random;
import com.nokia.mid.ui.FullCanvas;


class FireworksCanvas
    extends FullCanvas
    implements Runnable
{
    static final int FIXED_POINT_SHIFT = 8;

    private static final int MILLIS_PER_TICK = 100;
    private static final int TICKS_PER_EXPLOSION = 10;
    private static final int SPARK_LIFE_IN_TICKS = 20;
    private static final int SPARK_RANDOM_LIFE_IN_TICKS = 20;
    private static final int SPARK_VELOCITY = 2;
    private static final int[] colours =
    {
        0x00FF0000,  // red
        0x0000FF00,  // green
        0x00FFFF00,  // yellow
        0x00FF00FF,  // magenta
        0x0000FFFF,  // cyan
    };

    private final FireworksMIDlet parent;
    private final boolean useColour;
    private final Random random = new Random();
    private final DoublyLinkedList sparkList = new DoublyLinkedList();
    private volatile Thread animationThread = null;


    FireworksCanvas(FireworksMIDlet parent)
    {
        this.parent = parent;

        useColour = Display.getDisplay(parent).isColor();
    }


    synchronized void start()
    {
        animationThread = new Thread(this);
        animationThread.start();
    }


    synchronized void stop()
    {
        animationThread = null;
    }


    public void run()
    {
        Thread currentThread = Thread.currentThread();

        try
        {
            // This ends when animationThread is set to null, or when
            // it is subsequently set to a new thread; either way, the
            // current thread should terminate
            while (currentThread == animationThread)
            {
                long startTime = System.currentTimeMillis();
                tick();
                repaint(0, 0, getWidth(), getHeight());
                serviceRepaints();
                long timeTaken = System.currentTimeMillis() - startTime;
                if (timeTaken < MILLIS_PER_TICK)
                {
                    synchronized (this)
                    {
                        wait(MILLIS_PER_TICK - timeTaken);
                    }
                }
                else
                {
                    currentThread.yield();
                }
            }
        }
        catch (InterruptedException e)
        {
        }
    }


    public void paint(Graphics g)
    {
        g.setColor(0x00000000);    // black
        g.fillRect(0, 0, getWidth(), getHeight());
        drawAllSparks(g);
    }


    public void keyPressed(int keyCode)
    {
        // any softkey key-press exits
        if ((keyCode == KEY_SOFTKEY1) || (keyCode == KEY_SOFTKEY2) ||
            (keyCode == KEY_SOFTKEY3))
        {
            parent.exitRequested();
        }
    }


    private synchronized void tick()
    {
        Spark currentSpark = (Spark)(sparkList.getFirst());
        while (currentSpark != null)
        {
            Spark nextSpark = (Spark)(sparkList.getNext(currentSpark));
            currentSpark.tick();
            currentSpark = nextSpark;
        }

        if (sparkList.isEmpty() || (rand(TICKS_PER_EXPLOSION) == 0))
        {
            explode();
        }
    }


    private void explode()
    {
        int colour;

        if (useColour)
        {
            colour = colours[rand(colours.length)];
        }
        else
        {
            colour = 0x00FFFFFF;   // white
        }

        int x = (rand(getWidth() / 2) + getWidth() / 4) << FIXED_POINT_SHIFT;
        int y = (rand(getHeight() / 2) + getHeight() / 4) << FIXED_POINT_SHIFT;

        // Make 6*6 = 36 sparks, reasonably evenly distributed but with
        // a small amount of randomness
        for (int i = 0; i < 6; ++i)
        {
            for (int j = 0; j < 6; ++j)
            {
                int projectedAngle = i * 3 + rand(3);
                int planeAngle = j * 3 + rand(3);
                int magnitudeTimes256 =
                    SPARK_VELOCITY * cosineTimes256(planeAngle);

                int vx =
                    ((cosineTimes256(projectedAngle) * magnitudeTimes256) <<
                                                      FIXED_POINT_SHIFT) >> 16;
                int vy =
                    ((sineTimes256(projectedAngle) * magnitudeTimes256) <<
                                                      FIXED_POINT_SHIFT) >> 16;
                int timeToLive =
                    SPARK_LIFE_IN_TICKS + rand(SPARK_LIFE_IN_TICKS);
                Spark spark = new Spark(x, y,
                                        vx, vy,
                                        colour,
                                        timeToLive);
                sparkList.addFirst(spark);
            }
        }
    }


    private synchronized void drawAllSparks(Graphics g)
    {
        Spark currentSpark = (Spark)(sparkList.getFirst());
        while (currentSpark != null)
        {
            Spark nextSpark = (Spark)(sparkList.getNext(currentSpark));
            currentSpark.draw(g);
            currentSpark = nextSpark;
        }
    }


    private int rand(int scale)
    {
        return (random.nextInt() << 1 >>> 1) % scale;
    }


    // sines of angles 0, 10, 20, 30, 40, 50, 60, 70, 80, 90,    all *256
    private static final int[] SINES =
        { 0, 44, 88, 128, 165, 196, 222, 241, 252, 256 };


    // angle is in degrees/10, i.e. 0..36 for full circle
    private static int sineTimes256(int angle)
    {
        angle %= 36;    // 360 degrees
        if (angle <= 9)          // 0..90 degrees
        {
            return SINES[angle];
        }
        else if (angle <= 18)    // 90..180 degrees
        {
            return SINES[18-angle];
        }
        else if (angle <= 27)    // 180..270 degrees
        {
            return -SINES[angle-18];
        }
        else                     // 270..360 degrees
        {
            return -SINES[36-angle];
        }
    }


    // angle is in degrees/10, i.e. 0..36 for full circle
    private static int cosineTimes256(int angle)
    {
        return sineTimes256(angle + 9);     // i.e. add 90 degrees
    }
}

⌨️ 快捷键说明

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