PointerDemo.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- public class PointerDemo extends MIDlet {
- Display display;
- PointerCanvas pc;
- public PointerDemo() {
- display = Display.getDisplay(this);
- pc= new PointerCanvas();
- }
- public void startApp () {
- display.setCurrent(pc);
- }
- public void pauseApp() {
- }
- public void destroyApp (boolean unconditional) {
- pc=null;
- display=null;
- }
- class PointerCanvas extends Canvas {
- String iconFiles[]={"/ami.png", "/bigs.png","/ganma.png", "/knuckles.png","/sonic.png","/tails.png"};
- String iconLabels[]={"Ami", "Bigs", "Ganma", "Knuckles", "Sonic", "Tails"};
- Image icons[]= new Image[iconFiles.length];
- int nrow=3;
- int ncol=2;
- int width, height;
- int deltax, deltay;
- public PointerCanvas() {
- for(int i=0; i<iconFiles.length; i++) {
- try{
- icons[i]= Image.createImage(iconFiles[i]);
- }catch (Exception e) {
- System.out.println("Error: "+ e.getMessage());
- }
- }
- width=getWidth();
- height=getHeight();
- deltax=width/ncol;
- deltay=(height-20)/nrow;
- System.out.println(hasPointerEvents());
- System.out.println(hasPointerMotionEvents());
- }
- 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("请选择游戏人物:",0,0,g.TOP|g.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(int c=0; c<ncol; c++) {
- Image img= icons[r*ncol+c];
- g.drawImage(img, c*deltax+2, r*deltay+20,
- Graphics.LEFT | Graphics.TOP);
- g.drawRect(c*deltax,r*deltay+18,img.getWidth()+4,img.getHeight()+4);
- g.drawString(iconLabels[r*ncol+c], c*deltax+img.getWidth()+3,
- r*deltay+img.getHeight(), g.LEFT|g.BASELINE);
- }
- }
- }
- protected void pointerPressed(int x, int y) {
- int r=(y-20)/deltay;
- int c=x/deltax;
- int index=r*ncol+c;
- display.setCurrent(new ResultCanvas(this, iconLabels[index]));
- }
- }
- class ResultCanvas extends Canvas implements CommandListener {
- Displayable menu;
- Command backCommand = new Command("Back", Command.BACK, 1);
- String text;
- int width, height;
- public ResultCanvas(Displayable d, String message) {
- menu=d;
- text=message;
- width=getWidth();
- height=getHeight();
- this.addCommand(backCommand);
- this.setCommandListener(this);
- }
- protected void paint(Graphics g) {
- g.setColor(0xFFFFFF);
- g.fillRect(0, 0, width, height);
- g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
- g.setColor(0x000000);
- g.drawString("您选择了:"+text, 10, 10, g.TOP|g.LEFT);
- }
- public void commandAction (Command c, Displayable s) {
- if(c==backCommand) {
- display.setCurrent(menu);
- }
- }
- }
- }