PointerDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.midlet.*;
  2. import javax.microedition.lcdui.*;
  3. public class PointerDemo extends MIDlet {
  4.     Display display;
  5.     PointerCanvas  pc;
  6.     public PointerDemo() {
  7.         display = Display.getDisplay(this);
  8.         pc= new PointerCanvas();
  9.     }
  10.     public void startApp () {
  11.         display.setCurrent(pc);
  12.     }
  13.     public void pauseApp() {
  14.     }
  15.     public void destroyApp (boolean unconditional) {
  16.         pc=null;
  17.         display=null;
  18.     }
  19.     class PointerCanvas extends Canvas {
  20. String iconFiles[]={"/ami.png", "/bigs.png","/ganma.png", "/knuckles.png","/sonic.png","/tails.png"};
  21.         String iconLabels[]={"Ami", "Bigs", "Ganma", "Knuckles", "Sonic", "Tails"};
  22.         Image  icons[]= new Image[iconFiles.length];
  23.         int  nrow=3;
  24.         int  ncol=2;
  25.         int  width, height;
  26.         int deltax, deltay;
  27.         public PointerCanvas() {
  28.             for(int i=0; i<iconFiles.length; i++) {
  29.                 try{
  30.                     icons[i]= Image.createImage(iconFiles[i]);
  31.                 }catch (Exception e) {
  32.                     System.out.println("Error: "+ e.getMessage());
  33.                 }
  34.             }
  35.             width=getWidth();
  36.             height=getHeight();
  37.             deltax=width/ncol;
  38.             deltay=(height-20)/nrow;
  39.             System.out.println(hasPointerEvents()); 
  40.             System.out.println(hasPointerMotionEvents());
  41.         }
  42.         protected void paint(Graphics g) {
  43.             g.setColor(0xFFFFFF);
  44.             g.fillRect(0, 0, width, height);
  45.             g.setColor(0x000000);
  46.             g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
  47.             g.drawString("请选择游戏人物:",0,0,g.TOP|g.LEFT);
  48.             g.setColor(0x000000);
  49.             g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL));
  50.             for(int r=0; r<nrow; r++) {
  51.                 for(int c=0; c<ncol; c++) {
  52.                     Image img= icons[r*ncol+c];
  53.                     g.drawImage(img, c*deltax+2, r*deltay+20,
  54.                             Graphics.LEFT | Graphics.TOP);
  55.                     g.drawRect(c*deltax,r*deltay+18,img.getWidth()+4,img.getHeight()+4);
  56.                     g.drawString(iconLabels[r*ncol+c], c*deltax+img.getWidth()+3, 
  57.                                  r*deltay+img.getHeight(), g.LEFT|g.BASELINE);                   
  58.                 }
  59.             }
  60.         }
  61.         protected void pointerPressed(int x, int y) {
  62.             int r=(y-20)/deltay;
  63.             int c=x/deltax;
  64.             int index=r*ncol+c;
  65.             display.setCurrent(new ResultCanvas(this, iconLabels[index]));
  66.         }
  67.     }
  68.     class ResultCanvas extends Canvas implements CommandListener {
  69.         Displayable menu;
  70.         Command backCommand = new Command("Back", Command.BACK, 1);
  71.         String text;
  72.         int  width, height;
  73.         public ResultCanvas(Displayable d, String message) {
  74.             menu=d;
  75.             text=message;
  76.             width=getWidth();
  77.             height=getHeight();
  78.             this.addCommand(backCommand);
  79.             this.setCommandListener(this);
  80.         }
  81.         protected void paint(Graphics g) {
  82.             g.setColor(0xFFFFFF);
  83.             g.fillRect(0, 0, width, height);
  84.             g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
  85.             g.setColor(0x000000);            
  86.             g.drawString("您选择了:"+text, 10, 10, g.TOP|g.LEFT);
  87.         }
  88.          public void commandAction (Command c, Displayable s) {
  89.             if(c==backCommand) {
  90.                 display.setCurrent(menu);
  91.             }
  92.         }
  93.     }
  94. }