基于MIDP1.0实现动画效果

BSD盲区检测系统性能要求及试验方法 盲区监测系统BSD通过安装在车辆后方的两颗毫米波雷达实时监测驾驶员视野盲区,当发现视野盲区内出现其它道路使用者时,通过左右后视镜上的警示灯向驾驶员发出报警。 阅读详情
        在SUN的技术文章里面有一篇文章讲述了如何基于MIDP1.0实现动画,个人觉得很不错。在MIDP1.0中并为直接对动画提供支持,如果我们明白动画的原理,熟悉Timer和TimerTask的话实现动画并非很困难的事情。

       动画实际上就是一系列连续的帧,当他们变换的足够快的时候,我们人的眼睛就会觉得他是运动的。所以如果我们能够周期性的切换画面,从第一帧到第二帧到第三帧 ......那么我们就可以制作出动画的效果了。Timer和TimerTask正好可以很好的帮我们完成这一功能,如果你还不熟悉这两个类可以参考使用Java中的Timer和TimerTask。我们首先扩展TimerTask实现AnimatedImage类,它的功能就是在特定位置画图

import java.util.*;
import javax.microedition.lcdui.*;

// Defines an animated image, which is just a set
// of images of equal size which are drawn in turn
// to simulate movement.

public class AnimatedImage extends TimerTask
{
    private Canvas canvas;
    private Image[] images;
    private int[][] clipList;
    private int current;
    private int x;
    private int y;
    private int w;
    private int h;

    // Construct an animation with no canvas.

    public AnimatedImage(Image[] images)
    {
        this(null, images, null);
    }

    // Construct an animation with a null clip list.

    public AnimatedImage(Canvas canvas, Image[] images)
    {
        this(canvas, images, null);
    }

    // Construct an animation. The canvas can be null, but
    // if not null then a repaint will be triggered on it
    // each time the image changes due to a timer event.
    // If a clip list is specified, the image is drawn
    // multiple times, each time with a different clip
    // rectangle, to simulate transparent parts.

    public AnimatedImage(Canvas canvas, Image[] images, int[][] clipList)
    {
        this.canvas = canvas;
        this.images = images;
        this.clipList = clipList;

        if (images != null && clipList != null)
        {
            if (clipList.length < images.length)
            {
                throw new IllegalArgumentException();
            }
        }

        if (images != null && images.length > 0)
        {
            w = images[0].getWidth();
            h = images[0].getHeight();
        }
    }

    // Move to the next frame, wrapping if necessary.

    public void advance(boolean repaint)
    {
        if (++current >= images.length)
        {
            current = 0;
        }

        if (repaint && canvas != null && canvas.isShown())
        {
            canvas.repaint(x, y, w, h);
            canvas.serviceRepaints();
        }
    }

    // Draw the current image in the animation. If
    // no clip list, just a simple copy, otherwise
    // set the clipping rectangle accordingly and
    // draw the image multiple times.

    public void draw(Graphics g)
    {
        if (w == 0 || h == 0)
            return;

        int which = current;

        if (clipList == null || clipList[which] == null)
        {
            g.drawImage(images[which], x, y, g.TOP | g.LEFT);
        } else
        {
            int cx = g.getClipX();
            int cy = g.getClipY();
            int cw = g.getClipWidth();
            int ch = g.getClipHeight();

            int[] list = clipList[which];

            for (int i = 0; i + 3 <= list.length; i += 4)
            {
                g.setClip(x + list[0], y + list[1], list[2], list[3]);
                g.drawImage(images[which], x, y, g.TOP | g.LEFT);
            }

            g.setClip(cx, cy, cw, ch);
        }
    }

    // Moves the animation's top left corner.

    public void move(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    // Invoked by the timer. Advances to the next frame
    // and causes a repaint if a canvas is specified.

    public void run()
    {
        if (w == 0 || h == 0)
            return;

        advance(true);
    }
}

我们需要预先在jar文件里面存储几个大小一样,形态有序的图片,在MIDlet启动的时候把它装载在一个Image数组,并把它作为参数传递给AnimatedImage。
private Image[] loadFrames( String name, int frames )
                                  throws IOException {
    Image[] images = new Image[frames];
    for( int i = 0; i < frames; ++i ){
 images[i] = Image.createImage( name + i +
            ".png" );
    }

    return images;
}

Image[] frames = loadFrames( "/images/bird", 7 );
AnimatedImage ai = new AnimatedImage( frames );
ai.move( 20, 20 ); // set top-left to 20,20

下面是两个测试效果的AnimatedCanvas和AnimationTest类的源代码,在这里你可以得到全部的源文件和图片资源。仔细读读这几个类是很有好处的。有一点值得一提的是,在绘制图片的时候应该判断设备是否支持双缓冲,如果不支持要使用双缓冲技术避免画面闪烁。而且我们也不适宜把动画图片作的过大,这样会增大jar文件的大小,同时会给设备造成很大的负担。

    protected void paint(Graphics g)
    {
        Graphics saved = g;

        if (offscreen != null)
        {
            g = offscreen.getGraphics();
        }

        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());

        int n = images.size();
        for (int i = 0; i < n; ++i)
        {
            AnimatedImage img = (AnimatedImage) images.elementAt(i);
            img.draw(g);
        }

        if (g != saved)
        {
            saved.drawImage(offscreen, 0, 0, Graphics.LEFT | Graphics.TOP);
        }
    }

import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

// MIDlet that displays some simple animations.
// Displays a series of birds on the screen and
// animates them at different (random) rates.

public class AnimationTest extends MIDlet implements CommandListener
{

    private static final int BIRD_FRAMES = 7;
 private static final int NUM_BIRDS = 5;

    private Display display;
    private Timer timer = new Timer();
    private AnimatedImage[] birds;
    private Random random = new Random();

    public static final Command exitCommand = new Command("Exit", Command.EXIT,
            1);

    public AnimationTest()
    {
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == exitCommand)
        {
            exitMIDlet();
        }
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException
    {
        exitMIDlet();
    }

    public void exitMIDlet()
    {
        timer.cancel(); // turn it off...
        notifyDestroyed();
    }

    // Generate a non-negative random number...

    private int genRandom(int upper)
    {
        return (Math.abs(random.nextInt()) % upper);
    }

    public Display getDisplay()
    {
        return display;
    }

    // Initialize things by creating the canvas and then
    // creating a series of birds that are moved to
    // random locations on the canvas and attached to
    // a timer for scheduling.

    protected void initMIDlet()
    {
        try
        {
            AnimatedCanvas c = new AnimatedCanvas(getDisplay());
            Image[] images = loadFrames("/images/bird", BIRD_FRAMES);

            int w = c.getWidth();
            int h = c.getHeight();

            birds = new AnimatedImage[NUM_BIRDS];
            for (int i = 0; i < NUM_BIRDS; ++i)
            {
                AnimatedImage b = new AnimatedImage(c, images);
                birds[i] = b;
                b.move(genRandom(w), genRandom(h));
                c.add(b);
                timer.schedule(b, genRandom(1000), genRandom(400));
            }

            c.addCommand(exitCommand);
            c.setCommandListener(this);

            getDisplay().setCurrent(c);
        } catch (IOException e)
        {
            System.out.println("Could not load images");
            exitMIDlet();
        }
    }

    // Load the bird animation, which is stored as a series
    // of PNG files in the MIDlet suite.

    private Image[] loadFrames(String name, int frames) throws IOException
    {
        Image[] images = new Image[frames];
        for (int i = 0; i < frames; ++i)
        {
            images[i] = Image.createImage(name + i + ".png");
        }

        return images;
    }

    protected void pauseApp()
    {
    }

    protected void startApp() throws MIDletStateChangeException
    {
        if (display == null)
        {
            display = Display.getDisplay(this);
            initMIDlet();
        }
    }
}


import java.util.*;
import javax.microedition.lcdui.*;

// A canvas to which you can attach one or more
// animated images. When the canvas is painted,
// it cycles through the animated images and asks
// them to paint their current image.

public class AnimatedCanvas extends Canvas
{
    private Display display;
    private Image offscreen;
    private Vector images = new Vector();

    public AnimatedCanvas(Display display)
    {
        this.display = display;

        // If the canvas is not double buffered by the
        // system, do it ourselves...

        if (!isDoubleBuffered())
        {
            offscreen = Image.createImage(getWidth(), getHeight());
        }
    }

    // Add an animated image to the list.

    public void add(AnimatedImage image)
    {
        images.addElement(image);
    }

    // Paint the canvas by erasing the screen and then
    // painting each animated image in turn. Double
    // buffering is used to reduce flicker.

    protected void paint(Graphics g)
    {
        Graphics saved = g;

        if (offscreen != null)
        {
            g = offscreen.getGraphics();
        }

        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());

        int n = images.size();
        for (int i = 0; i < n; ++i)
        {
            AnimatedImage img = (AnimatedImage) images.elementAt(i);
            img.draw(g);
        }

        if (g != saved)
        {
            saved.drawImage(offscreen, 0, 0, Graphics.LEFT | Graphics.TOP);
        }
    }
}


< p>
【Zephyr存储专题】18_从零教你开发MMC卡驱动(寄存器篇) 本文深入探讨eMMC/SDMMC控制器硬件结构及寄存器配置,为驱动开发提供技术参考。重点解析eMMC内部结构(Flash阵列、控制器、接口)和SDMMC关键寄存器(控制寄存器CR、命令寄存器CMDR等),详细说明各寄存器功能位域,包括时钟控制、DMA配置、命令传输等核心操作。文章还介绍了数据线配置和多线并行传输机制,为开发者实现卡驱动和文件系统适配提供实用指导。 阅读详情

相关推荐

BSL编程语言 —— 系统化程序设计

BSL编程语言简介 文章目录0. 选择编程语言1. 写简单的表达式(Expression) 0. 选择编程语言 如果你第一次使用DrRacket,打开后需要选择Beginning Student Language (BSL) Language -> Choose Language (或直接按Ctrl + L),看到如下洁面后选择Beginning Student,点击OK 定义区【De...

CSDoneRight的博客 6452

基于MIDP1.0实现通信录

作者:mingjava  文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=41项目简介      基于MIDP1.0实现的个人通信录是我在学习MIDP子系统Record Management System的时候自己编写的应用程序,整个应用程序涉及到MIDP的高级和低级API、应用MVC实现界面导航、RMS的高级应用、多线程

mobilechannel的专栏 940

VFIO框架源码分析(二十一)- IOMMUFD 模式下的 DMA 映射机制

在虚拟化 I/O 的世界里,无论上层管理面(如对象模型、设备绑定)如何重构,数据面最核心的任务始终未变——DMA 映射(DMA Mapping)。即如何建立 Guest Physical Address (GPA) 到 Host Physical Address (HPA) 的映射关系,并将其填入 IOMMU 的硬件页表中,使得直通设备能够正确访问虚拟机内存。在 Legacy VFIO (Type1) 时代,这一职责由承担,它通过ioctl 操作 Container。

qq_36071928的博客 1019

在MIDP1.0下实现图片翻转

    我们知道,MIDP1.0中并没有提供图片翻转的功能。但是在MIDP2.0中,我们可以利用Sprite的setTransform(int transform)方法来达到目的。在该方法中,transform参数的取值范围为8个常量之一,这8个常量分别是:    Sprite.TRANS_MIRROR:让精灵左右镜像变换    Sprite.TRANS_MIRROR_ROT180: 让精灵左右镜

Stone的专栏 2392

超详细neo4j安装教程

一、neo4j简介 最近开始学习知识图谱,所以首先想先学习一下neo4j的使用。 Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。由于知识图谱中存在大量的关系型信息(实体—关系—实体), 使用结构化数据库进行存储将产生大量的冗余存储信息, 因此将图数据库作为知识图谱的存储容器成为流行的选择。当前较为常用的图数据库主要有 Neo4j 等。 二、neo4j的安装 功能快捷键 撤销:Ctrl/Command + Z 重做:Ctrl/Command + Y 加粗:Ctrl/Co

qq_38335648的博客 9万+

30分钟搭建AI科研写作工作台:基于WorkBuddy的论文全流程加速实践

在人工智能技术日益普及的今天,如何利用智能工具提升专业领域工作效率成为关键课题。其核心原理在于通过模块化设计,将复杂的任务流程拆解为可复用、可编排的标准化技能单元,并结合精准的指令控制,实现从通用对话到深度任务执行的转变。这一技术价值在于显著降低重复性劳动强度,将人的创造力聚焦于核心决策与创新环节。在科研写作、数据分析、代码开发等场景中,这类智能工作台能有效辅助完成文献调研、内容生成、格式校验等繁琐步骤。本文以科研论文写作为具体应用场景,深入探讨如何利用WorkBuddy这一智能工作台,通过配置核心科研技能

weixin_29032489的博客 1742

midp1.0动画

在SUN的技术文章里面有一篇文章讲述了如何基于MIDP1.0实现动画,个人觉得很不错。在MIDP1.0中并为直接对动画提供支持,如果我们明白动画的原理,熟悉Timer和TimerTask的话实现动画并非很困难的事情。    动画实际上就是一系列连续的帧,当他们变换的足够快的时候,我们人的眼睛就会觉得他是运动的。所以如果我们能够周期性的切换画面,从第一帧到第二帧到第三帧 ......那么我们就可

335

Python量化入门:用pytdx获取800个交易日K线数据的完整指南(附代码)

本文为Python量化交易初学者提供了使用pytdx库免费获取股票日线级别K线数据的完整指南。文章详细介绍了从环境搭建、单只股票数据获取,到构建健壮的批量下载器、数据清洗与验证,再到基础分析与可视化的全流程,并附有可直接运行的代码示例,帮助读者快速建立本地股票数据库。

weixin_29050513的博客 1489

MIDP中的动画

作者:Eir Giguere 编译:Sean 使用MIDP(Mobile Information Device Profile)的开发人员经常会抱怨用些什么办法才可以在一个MIDlet上显示动画。 MIDP 1.0 没有直接提供对动画的支持(正在开发中的MIDP 2.0支持),但真要是自己去实现,其实也并非是一件很难的事。 任何动画的最基本的前提,是要在足够快的时间内显示和更换一张张的图片,让人的

gdsean的专栏 922

8、对比 LightGBM、XGBoost 和深度学习

本文对比了LightGBM、XGBoost和深度学习模型(特别是TabTransformer)在处理表格数据时的性能差异。从模型复杂度、数据准备、训练时间、准确性和适用场景等方面进行分析,重点探讨了三者在人口普查收入预测任务中的表现。文章详细介绍了XGBoost与LightGBM的核心特性与区别,深入讲解了TabTransformer的架构与实现,并涵盖了分类特征编码、缺失值处理和类别不平衡等关键数据预处理技术,为选择合适的表格数据建模方法提供了实践指导。

julia4scientist的博客 122

ARMv8/ARMv9的TLB深度学习

千万不要人云亦云。一个TLB entry里有很多很多东西的,有100多个比特位呢,并不是仅仅只有VA/PA/nG/VMID/ASID等。.👉👉👉 个人博客笔记导读目录(全部) 👈👈👈。

baron-周贺贺-代码改变世界ctw 1571

用J2ME在移动设备上实现动画

使用MIDP(Mobile Information Device Profile)的开发人员经常会抱怨用些什么办法才可以在一个MIDlet上显示动画。MIDP 1.0 没有直接提供对动画的支持(MIDP 2.0支持),但真要是自己去实现,其实也并非是一件很难的事。   任何动画的最基本的前提,是要在足够快的时间内显示和更换一张张的图片,让人的眼睛看到动的画面效果。图片必须按照顺序画出来。从一张图片

Legion.Tao(http://t.sina.com.cn/dastantao) 649

C# 上位机程序中的数据采集与存储技术:从采集到持久化的完整解决方案

本文系统讲解C#上位机程序如何实现设备数据采集与持久化。主要内容包括:1)多种数据采集方式(串口通信、Modbus、OPC等)及代码示例;2)详细对比不同存储技术(CSV、SQLite、关系型数据库等)的优缺点;3)提供SQLite等存储实现代码;4)推荐分层系统架构(采集-缓存-存储-可视化);5)性能优化建议(多线程采集、异步存储、批量写入等)。文章强调应根据项目需求选择合适的技术方案,构建高效稳定的数据采集存储闭环系统。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1031

MIDP2.0 Game API简介

MIDP 2.0 为移动开发者带来几项新特性,比如支持媒体、更好的用户界面、新的连通协议、enabled push、空中下载和更好的安全性。然而一个最让人感兴趣的新特性就是GAME API。这篇文章重点介绍GAME API里的新类和它们的用法,开发环境使用J2ME Wireless Toolkit 2.0 Beta。The Game APIThe Game API 帮助开发者(游戏开发者或其他想得

冷枫@CSDN 2937

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

AprilGameEngine_MIDP1.0

AprilGameEngine_MIDP1.0Author:Tony.MingEmail: yesming@126.comCopy Right by tony.ming 2004版权声明Licence:----------------------------此软件为共享软件,任何个人和单位均可免费复制,拷贝,复制时请注明出处。但如需商业用途或者使用,修改其中的全部或者部分代码,图片。请先和

1873

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

上一篇: 精通J2ME中的Hello World
下一篇: 在MIDP2.0中调用平台服务
mingjava
博客等级 码龄24年 68粉丝 114原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址