网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > J2EE/J2ME
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.J2EE 探险者:用于无状态网络的 .
.Servlet中jdbc应用高级篇之一.
.J2EE部署中的下一个冲击波.
.JavaMail快速入门-8.
.Log4j深入浅出.
.赛车游戏(二)一个Mascot Capsule.
.漫谈EJB (3).
.一个简单的网络客户端.
.构建高性能J2EE应用的五种核心策.
.基于MIDP1.0实现RMS容量探测器.
.在Spring中配置Collection.
.如何在鸡尾酒会上谈论Jini,J2EE.
.使用SAAJ1.2发送和接收二进制Web.
.熟练使用J2ME可选包MMAPI.
.sitemesh的问题及解决.
.M3GConverter使用手册.
.Servlet中jdbc应用高级篇之六.
.使用Servlet发送电子邮件.
.赛车游戏(一)通过蓝牙实现两个手.
.使用Servlet动态生成图片.

使用Timer和Canvas制作动画效果

发表日期:2007-12-23


本例演示了如何使用Canvas和Timer来制作动画效果,可以使用Guage来调节帧速率和动画精灵的个数。程序结构清晰,是不错的参考案例。

使用Timer和Canvas制作动画效果(图一)使用Timer和Canvas制作动画效果(图二)

/*
 * AnimationMIDlet.Java
 *
 * Created on 2005年12月2日, 上午10:50
 */

package com.j2medev.animation;

import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class AnimationMIDlet extends MIDlet implements CommandListener, ItemStateListener {
   
    // The MIDlet's Display object
    private Display display;
    // Flag indicating first call of startApp
    protected boolean started;
    // Exit command
    private Command exitCommand;
    // Setup command
    private Command setupCommand;
    // Run command
    private Command runCommand;
    // Configuration form
    private Form form;
    // Animation canvas
    private AnimationCanvas canvas;
    // Gauge for block count
    private Gauge blockGauge;
    // Gauge for frame rate
    private Gauge rateGauge;
    // Initial frame rate
    private static final int FRAME_RATE = 1;
    // Initial number of blocks
    private static final int BLOCK_COUNT = 1;
   
    protected void startApp() {
        if (!started) {
            display = Display.getDisplay(this);
            form = new Form("Animation");
            rateGauge = new Gauge("Frame rate", true, 10, FRAME_RATE);
            blockGauge = new Gauge("Blocks", true, 4, BLOCK_COUNT);
            form.append(rateGauge);
            form.append(blockGauge);
            form.setItemStateListener(this);
            canvas = createAnimationCanvas();
            exitCommand = new Command("Exit", Command.EXIT, 0);
            setupCommand = new Command("Setup", Command.SCREEN, 0);
            runCommand = new Command("Run", Command.SCREEN, 0);
            canvas.addCommand(exitCommand);
            canvas.addCommand(setupCommand);
            form.addCommand(exitCommand);
            form.addCommand(runCommand);
            form.setCommandListener(this);
            canvas.setCommandListener(this);
            display.setCurrent(form);
            started = true;
        }
    }
   
    protected void pauseApp() {
    }
   
    protected void destroyApp(boolean unconditional) {
    }
   
    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            notifyDestroyed();
        } else if (c == runCommand) {
            display.setCurrent(canvas);
        } else if (c == setupCommand) {
            display.setCurrent(form);
        }
    }
    public void itemStateChanged(Item item) {
        if (item == blockGauge) {
            int count = blockGauge.getValue();
            if (count < 1) {
                count = 1;
            }
            canvas.setBlockCount(count);
        } else if (item == rateGauge) {
            int count = rateGauge.getValue();
            if (count < 1) {
                count = 1;
            }
            canvas.setFrameRate(count);
        }
    }
    // Creates the canvas that will draw the block
    protected AnimationCanvas createAnimationCanvas() {
        return new AnimationCanvas();
    }
    class AnimationCanvas extends Canvas {
       
        // Size of each block
        protected static final int SIZE = 6;
        // Initial speeds in the X direction
        protected final int[] xSpeeds = { 2, -2, 0, -2 };
        // Initial speeds in the Y direction
        protected final int[] ySpeeds = { 2, -2, 2, -0 };
        // Background color
        protected int background = display.isColor() ? 0xff0ff0 : 0xc0c0c0;
        // Foreground color
        protected int foreground = display.isColor() ? 0xfff000 : 0;
        // Width of screen
        protected int width = getWidth();
        // Height of screen
        protected int height = getHeight();
        // The screen update rate
        protected int frameRate;
        // The blocks to draw on the screen
        protected Block[] blocks;
        // The update timer
        protected Timer timer;
        // The update timer task
        protected TimerTask updateTask;
        // Gets the maximum number of blocks
        public int getMaxBlocks() {
            return blocks.length;
        }
        // ConstrUCts a canvas with default settings
        AnimationCanvas() {
            setBlockCount(BLOCK_COUNT);
            setFrameRate(FRAME_RATE);
        }
        // Sets the number of blocks to draw
        public void setBlockCount(int count) {
            if (count > xSpeeds.length) {
                throw new IllegalArgumentException("Cannot have more than "
                        + xSpeeds.length + " blocks");
            }
            blocks = new Block[count];
            createBlocks();
        }
        // Gets the number of blocks to draw
        public int getBlockCount() {
            return blocks.length;
        }
        // Sets the number of updates per second
        public void setFrameRate(int frameRate) {
            if (frameRate < 1 frameRate > 10) {
                throw new IllegalArgumentException("Frame rate must be > 0 and <= 10");
            }
            this.frameRate = frameRate;
            if (isShown()) {
                startFrameTimer();
            }
        }
        // Gets the number of updates per second
        public int getFrameRate() {
            return frameRate;
        }
        // Paint canvas background and all
        // of the blocks in their correct locations.
        protected void paint(Graphics g) {
            // Paint with the background color
            g.setColor(background);
            g.fillRect(0, 0, width, height);
            // Draw all of the blocks
            g.setColor(foreground);
            synchronized (this) {
                for (int i = 0, count = blocks.length; i < count; i++) {
                    g.fillRect(blocks[i].x, blocks[i].y, SIZE, SIZE);
                }
            }
        }
        // Notification that the canvas has been made visible
        protected void showNotify() {
            // Start the frame timer running
            startFrameTimer();
        }
        // Notification that the canvas is no longer visible
        protected void hideNotify() {
            // Stop the frame timer
            stopFrameTimer();
        }
        // Creates the blocks to be displayed
        private void createBlocks() {
            int startX = (width - SIZE)/2;
            int startY = (height - SIZE)/2;
            for (int i = 0, count = blocks.length; i < count; i++) {
                blocks[i] = new Block(startX, startY, xSpeeds[i], ySpeeds[i]);
            }
        }
        // Starts the frame redraw timer
        protected void startFrameTimer() {
            timer = new Timer();
            updateTask = new TimerTask() {
                public void run() {
                    moveAllBlocks();
                }
            };
            long interval = 1000/frameRate;
            timer.schedule(updateTask, interval, interval);
        }
        // Stops the frame redraw timer
        protected void stopFrameTimer() {
            timer.cancel();
        }
        // Called on eXPiry of timer.
        public synchronized void moveAllBlocks() {
            // Update the positions and speeds
            // of all of the blocks
            for (int i = 0, count = blocks.length; i < count; i++) {
                blocks[i].move();
                // Request a repaint of the screen
                repaint();
            }
        }
        // Inner class used to represent a block on the screen
        class Block {
            int x;      // X position
            int y;      // Y position
            int xSpeed; // Speed in the X direction
            int ySpeed; // Speed in the Y direction
            Block(int x, int y, int xSpeed, int ySpeed) {
                this.x = x;
                this.y = y;
                this.xSpeed = xSpeed;
                this.ySpeed = ySpeed;
            }
            void move() {
                x += xSpeed;
                if (x <= 0 x + SIZE >= width) {
                    xSpeed = -xSpeed;
                }
                y += ySpeed;
                if (y <= 0 y + SIZE >= height) {
                    ySpeed = -ySpeed;
                }
            }
        }
    }
}


(出处:)


上一篇:在Canvas上绘制可修改的图片 人气:710
下一篇:基于MIDP1.0实现屏幕滚动 人气:809
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐