Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226816
  • 博文数量: 45
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 915
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 16:03
文章分类

全部博文(45)

文章存档

2011年(1)

2008年(44)

我的朋友

分类: Java

2008-07-17 15:49:39

采用低级界面实现,手机触摸屏事件--图片切换。
注:你的手机必须支持触摸屏或智能手机一类.
代码如下:
 
 

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) {        
    }
    
    
    
    
        
}

阅读(1369) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~