⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gmcanvas.java

📁 j2me全触摸屏示例代码——一款简单的触摸屏游戏
💻 JAVA
字号:
/*
 * GmCanvas.java
 *
 * Created on 2007年7月6日, 下午5:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


/*
 * 得在模拟器目录下wtklib\devices\DefaultColorPhone\DefaultColorPhone.properties
 * 文件中修改 touch_screen=false 为 touch_screen=true 
 * 
 * 	#	Touchscreen support
	###############
    touch_screen=true  
 */

import javax.microedition.lcdui.*;

/**
 * @author fobme
 */
public class GmCanvas extends Canvas
{
    private String iconFiles[] = {"1.png","2.png","3.png","4.png","5.png","6.png"};
    private String iconLabels[] = {"1","2","3","4","5","6"};
    Image icons[] = new Image[iconFiles.length];
    private  int width, height;
    int nrow = 3; // 行
    int ncol = 2; // 列
    int deltax, deltay;

    /** Creates a new instance of GmCanvas */
    public GmCanvas ()
    {
        for(int i = 0; i<iconFiles.length; i++)
        {
            try
            {
                icons[i] = Image.createImage ("/res/"+iconFiles[i]); // 到当前的路径下加载图像
            }
            catch (Exception e)
            {
                System.out.println ("Error:"+e.getMessage ());
            }
        }

        width =  getWidth();
        height = getHeight();
        deltax = width/ncol;  // 每列的列大小(列距)
        deltay = height/nrow; // 每行的行大小(行距)

        System.out.println ("是否支持手写笔触屏事件:" + hasPointerEvents());
        System.out.println ("是否支持手写笔在屏幕上移动产生pointerDragged()方法:" + hasPointerMotionEvents());
        
    }
    
    public void paint(Graphics g)
    {
        g.setColor (0xFFFFFF);
        g.fillRect (0,0,width,height);
        g.setColor (0x000000);
        g.setFont ( Font.getFont ( Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE ) );
        g.drawString ("please choose the photo:", 0, 0, Graphics.TOP|Graphics.LEFT);
        g.setColor (0x000000);
        g.setFont ( Font.getFont ( Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL ) );

        for(int r = 0; r<nrow; r++) // 此处for循环语句将图片绘制在屏幕上
        {
            for(int c = 0; c<ncol; c++)
            {
                Image img = icons[r * ncol + c];
                g.drawImage (img, c * deltax + 2, r * deltay + 60, Graphics.LEFT|Graphics.TOP);
                g.drawRect (c * deltax, r * deltay + 58, img.getWidth () + 4, img.getHeight () + 4);
                g.drawString (iconLabels[r * ncol + c], c* deltax + img.getWidth () + 10, 
                        r * deltay + img.getHeight ()+60,  Graphics.LEFT|Graphics.BASELINE);
            }
        }
    }
    
    protected void pointerPressed(int x, int y) // 手写笔点击屏幕产生的事件
    {
        int r = (y-20)/deltay;
        int c = x/deltax;
        int index = r * ncol + c;  //判断出第几个图片被点击

        touchScreenMIDlet.display.setCurrent (new ResultCanvas(this, iconLabels[index]));
    }
    
    class ResultCanvas extends Canvas
    {

        String string;
        GmCanvas gmcanvas;

        public ResultCanvas(GmCanvas gmcanvas, String string)
        {
            this.string = string;
            this.gmcanvas = gmcanvas;
        }
        
        protected void paint(Graphics g)
        {
            g.setColor (0xFFFFFF);
            g.fillRect (0,0,width,height);
            g.setColor (0x000000);
            g.setFont ( Font.getFont ( Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE ) );
            g.drawString ("你选择的图片是:" + string, 10, 10, Graphics.TOP|Graphics.LEFT );
        }

    }
    

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -