Chinaunix

标题: j2me实现触摸屏图片切换 [打印本页]

作者: AnyCallStar    时间: 2008-07-17 15:49
标题: j2me实现触摸屏图片切换
采用低级界面实现,手机触摸屏事件--图片切换。
注:你的手机必须支持触摸屏或智能手机一类.
代码如下:


import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Ticker;
import javax.microedition.lcdui.*;
public class CanvasImage extends Canvas implements CommandListener {
   
    int start_x = 0;
    int start_y = 0;
    int end_x = 0;
    int end_y = 0;
    private String imgurl = null;
    private Image im1 = null;
    public CanvasImage() {
        Ticker ticker = new Ticker("欢迎使用**手机移动终端");
        this.setTicker(ticker);
        imgurl = "/6-1.png";   
    }
   
    protected void paint(Graphics g) {        
        try {
            im1 = Image.createImage(imgurl); //加载第一张图片
            //清屏
            g.setColor(255,255,255);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.drawImage(im1, this.getWidth() / 2, this.getHeight() / 2,
                    Graphics.HCENTER | Graphics.BOTTOM);               
        } catch (IOException e) {
            e.printStackTrace();
        }            
    }
    /*
     * (非 Javadoc)鼠标点击切换图片
     * @see javax.microedition.lcdui.Canvas#pointerPressed(int, int)
     */
    public void pointerPressed(int x, int y) {
        start_x = x;
        start_y = y;
        if(x > 85 && y > 55 && x  150 && y  130) {
            if (imgurl == "/6-1.png") {
                imgurl = "/6-2.png";
            } else {
                imgurl = "/6-1.png";
            }
        }
        repaint();
        this.serviceRepaints();
    }
   
    public void pointerDragged(int x, int y) {
        end_x = x;
        end_y = y;
        repaint();
    }
   
    public void pointerReleased(int x, int y) {
        end_x = x;
        end_y = y;
    }
    public void commandAction(Command cmd, Displayable dis) {        
    }
   
   
   
   
        
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/43513/showart_1084554.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2