分享
 
 
 

使用Timer和Canvas制作动画效果

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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

/*

* 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;

}

}

}

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有