首页 > 学院 > 开发设计 > 正文

基于MIDP1.0实现屏幕滚动

2019-11-18 16:05:51
字体:
来源:转载
供稿:网友

本例介绍了一个常用的编程技巧,就是如何基于MIDP1.0实现屏幕的滚动效果。在MIDP2.0可以借助LayerManager的的“可视窗口”来实现。MIDP1.0通过变化坐标系来实现屏幕的滚动效果,只要借助Graphics.translate()方法。

基于MIDP1.0实现屏幕滚动

package com.j2medev.translate;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Translate extends MIDlet
{
  PRivate Display  display;     // The display
  private TranslateCanvas canvas;   // Canvas
  public Translate()
  {
    display = Display.getDisplay(this);
    canvas  = new TranslateCanvas(this);
  }
  protected void startApp()
  {
    display.setCurrent( canvas );
  }
  protected void pauseApp()
  { }

  protected void destroyApp( boolean unconditional )
  { }
  public void exitMIDlet()
  {
    destroyApp(true);
    notifyDestroyed();
  }
}

/*
 * TranslateCanvas.java
 *
 * Created on 2005年12月1日, 下午10:15
 *
 * To change this template, choose Tools Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.j2medev.translate;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

class TranslateCanvas extends Canvas implements CommandListener {
    private Command cmExit;
    private Translate midlet;
    private Image im = null;
    private int translatex = 0, translatey = 0;
    public TranslateCanvas(Translate midlet) {
        this.midlet = midlet;
        cmExit = new Command("Exit", Command.EXIT, 1);
        addCommand(cmExit);
        setCommandListener(this);
       
        try {
            //创建图片
            im = Image.createImage("/img.png");
        } catch (Java.io.IOException e) {
            System.err.println("Unable to locate or read .png file");
        }
    }
   
    protected void paint(Graphics g) {
        if (im != null) {
            //清屏
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, getWidth(), getHeight());
            //变换坐标,请参考Graphics.translate(x,y)方法说明
            g.translate(translatex, translatey);
            //总是在原点处画图片,由于前面变换了坐标 看起来同样图片会动起来
            g.drawImage(im, 0, 0, Graphics.LEFT Graphics.TOP);
        }
    }
   
    public void commandAction(Command c, Displayable d) {
        if (c == cmExit)
            midlet.exitMIDlet();
    }
    protected void keyPressed(int keyCode) {
        switch (getGameAction(keyCode)) {
            case UP:
                break;
            case DOWN:
                break;
            case LEFT:
                // 图片右边缘已经到达屏幕最右边
                System.out.println(translatex);
                System.out.println(im.getWidth());
                if (translatex + im.getWidth() < getWidth())
                    return;//直接返回
                else
                    translatex -= 2;
                break;
               
            case RIGHT:
                // 滚动到图片的左边缘
                if (translatex>getWidth()-2)
                    return;
                else
                    translatex += 2;
                break;
        }
        repaint();
    }
}
下载源码变换坐标


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



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表