MainCanvas.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:8k
源码类别:
J2ME
开发平台:
Java
- package exmenu;
- /**
- * <p>Title: ExMenu</p>
- *
- * <p>Description: lizhenpeng</p>
- *
- * <p>Copyright: Copyright (c) 2005</p>
- *
- * <p>Company: LP&P</p>
- *
- * @author lipeng
- * @version 1.0
- */
- import javax.microedition.lcdui.*;
- class MainCanvas
- extends Canvas implements Runnable
- {
- private int width;
- private int height;
- private long startTime;
- private long endTime;
- private static final int FRAME_TIME=80;
- private volatile Thread mainThread=null;
- public int whichActive;
- public int allAction;
- private Image image;
- private boolean isVolOn;
- private Font font=Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_BOLD,
- Font.SIZE_LARGE);
- public MainCanvas()
- {
- this.width=this.getWidth();
- this.height=this.getHeight();
- start();
- whichActive = 0;
- try
- {
- image=Image.createImage("/bg.png");
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- public void paint(Graphics g)
- {
- drawMenuScreen(g);
- }
- public void drawMenuScreen(Graphics g)
- {
- int menuOffset=(height-font.getHeight()*3)/2;
- g.setFont(font);
- g.setClip(0,0,width,height);
- g.drawImage(image,0,0,g.LEFT|g.TOP);
- int item0Color=0;
- int item1Color=0;
- int item2Color=0;
- switch(whichActive)
- {
- case 0:
- item0Color=255<<16;
- break;
- case 1:
- item1Color=255<<16;
- break;
- case 2:
- item2Color=255<<16;
- break;
- }
- g.setColor(item0Color);
- g.drawString("新游戏",(width-font.stringWidth("新游戏"))/2,
- menuOffset+font.getHeight()*0,g.LEFT|g.TOP);
- g.setColor(item1Color);
- g.drawString("载入进度",(width-font.stringWidth("载入进度"))/2,
- menuOffset+font.getHeight()*1,g.LEFT|g.TOP);
- g.setColor(item2Color);
- if(isVolOn)
- {
- g.drawString("声音开",(width-font.stringWidth("声音开"))/2,
- menuOffset+font.getHeight()*2,g.LEFT|g.TOP);
- } else
- {
- g.drawString("声音关",(width-font.stringWidth("声音关"))/2,
- menuOffset+font.getHeight()*2,g.LEFT|g.TOP);
- }
- }
- public void start()
- {
- mainThread=new Thread(this);
- mainThread.start();
- }
- public void run()
- {
- try
- {
- Thread currentThread=Thread.currentThread();
- while(currentThread==mainThread)
- {
- startTime=System.currentTimeMillis();
- action();
- repaint(0,0,width,height);
- serviceRepaints();
- endTime=System.currentTimeMillis();
- if((endTime-startTime)<FRAME_TIME)
- {
- Thread.sleep(FRAME_TIME-(endTime-startTime));
- }
- }
- } catch(InterruptedException ie)
- {
- System.out.println(ie.toString());
- }
- }
- protected void keyPressed(int keyCode)
- {
- judgeKeyCode(keyCode,getGameAction(keyCode));
- }
- protected void keyReleased(int keyCode)
- {
- freeKey(keyCode,getGameAction(keyCode));
- }
- public void judgeKeyCode(int keyCode,int GameKeyCode)
- {
- switch(GameKeyCode)
- {
- case Canvas.FIRE:
- if(((allAction&LPKeyMask.MASK_KEY_OK)==0)
- &&((allAction&LPKeyMask.MASK_KEY_OK_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_OK|LPKeyMask.MASK_KEY_OK_FLAG;
- }
- break;
- case Canvas.UP:
- if(((allAction&LPKeyMask.MASK_KEY_UP)==0)
- &&((allAction&LPKeyMask.MASK_KEY_UP_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_UP|LPKeyMask.MASK_KEY_UP_FLAG;
- }
- break;
- case Canvas.DOWN:
- if(((allAction&LPKeyMask.MASK_KEY_DOWN)==0)
- &&((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_DOWN|LPKeyMask.MASK_KEY_DOWN_FLAG;
- }
- break;
- case Canvas.LEFT:
- if(((allAction&LPKeyMask.MASK_KEY_LEFT)==0)
- &&((allAction&LPKeyMask.MASK_KEY_LEFT_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_LEFT|LPKeyMask.MASK_KEY_LEFT_FLAG;
- }
- break;
- case Canvas.RIGHT:
- if(((allAction&LPKeyMask.MASK_KEY_RIGHT)==0)
- &&((allAction&LPKeyMask.MASK_KEY_RIGHT_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_RIGHT|LPKeyMask.MASK_KEY_RIGHT_FLAG;
- }
- break;
- default:
- switch(keyCode)
- {
- case Canvas.KEY_NUM2: //up
- if(((allAction&LPKeyMask.MASK_KEY_UP)==0)
- &&((allAction&LPKeyMask.MASK_KEY_UP_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_UP|LPKeyMask.MASK_KEY_UP_FLAG;
- }
- break;
- case Canvas.KEY_NUM8: //down
- if(((allAction&LPKeyMask.MASK_KEY_DOWN)==0)
- &&((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_DOWN|LPKeyMask.MASK_KEY_DOWN_FLAG;
- }
- break;
- case Canvas.KEY_NUM4: //left
- if(((allAction&LPKeyMask.MASK_KEY_LEFT)==0)
- &&((allAction&LPKeyMask.MASK_KEY_LEFT_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_LEFT|LPKeyMask.MASK_KEY_LEFT_FLAG;
- }
- break;
- case Canvas.KEY_NUM6: //right
- if(((allAction&LPKeyMask.MASK_KEY_RIGHT)==0)
- &&((allAction&LPKeyMask.MASK_KEY_RIGHT_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_RIGHT|LPKeyMask.MASK_KEY_RIGHT_FLAG;
- }
- break;
- case Canvas.KEY_NUM5: //ok
- if(((allAction&LPKeyMask.MASK_KEY_OK)==0)
- &&((allAction&LPKeyMask.MASK_KEY_OK_FLAG)==0))
- {
- allAction|=LPKeyMask.MASK_KEY_OK|LPKeyMask.MASK_KEY_OK_FLAG;
- }
- break;
- }
- break;
- }
- }
- public void freeKey(int keyCode,int gameKeyCode)
- {
- switch(gameKeyCode)
- {
- case Canvas.FIRE:
- allAction&=~LPKeyMask.MASK_KEY_OK;
- break;
- case Canvas.UP:
- allAction&=~LPKeyMask.MASK_KEY_UP;
- break;
- case Canvas.DOWN:
- allAction&=~LPKeyMask.MASK_KEY_DOWN;
- break;
- case Canvas.LEFT:
- allAction&=~LPKeyMask.MASK_KEY_LEFT;
- break;
- case Canvas.RIGHT:
- allAction&=~LPKeyMask.MASK_KEY_RIGHT;
- break;
- default:
- switch(keyCode)
- {
- case Canvas.KEY_NUM2: //up
- allAction&=~LPKeyMask.MASK_KEY_UP;
- break;
- case Canvas.KEY_NUM8: //down
- allAction&=~LPKeyMask.MASK_KEY_DOWN;
- break;
- case Canvas.KEY_NUM4: //left
- allAction&=~LPKeyMask.MASK_KEY_LEFT;
- break;
- case Canvas.KEY_NUM6: //right
- allAction&=~LPKeyMask.MASK_KEY_RIGHT;
- break;
- case Canvas.KEY_NUM5:
- allAction&=~LPKeyMask.MASK_KEY_OK;
- break;
- }
- break;
- }
- }
- public void action()
- {
- if((allAction&LPKeyMask.MASK_KEY_UP_FLAG)!=0)
- {
- allAction&=~LPKeyMask.MASK_KEY_UP_FLAG;
- if(whichActive!=0)
- {
- --whichActive;
- } else
- {
- whichActive=2;
- }
- } else if((allAction&LPKeyMask.MASK_KEY_DOWN_FLAG)!=0)
- {
- allAction&=~LPKeyMask.MASK_KEY_DOWN_FLAG;
- if(whichActive!=2)
- {
- ++whichActive;
- } else
- {
- whichActive=0;
- }
- } else if((allAction&LPKeyMask.MASK_KEY_OK_FLAG)!=0)
- {
- allAction&=~LPKeyMask.MASK_KEY_OK_FLAG;
- switch(whichActive)
- {
- case 0:
- break;
- case 1:
- break;
- case 2:
- isVolOn=!isVolOn;
- break;
- }
- }
- }
- }
- class LPKeyMask
- {
- public static final int MASK_KEY_LEFT=0x0001;
- public static final int MASK_KEY_LEFT_FLAG=0x0100;
- public static final int MASK_KEY_RIGHT=0x0002;
- public static final int MASK_KEY_RIGHT_FLAG=0x0200;
- public static final int MASK_KEY_UP=0x0004;
- public static final int MASK_KEY_UP_FLAG=0x0400;
- public static final int MASK_KEY_DOWN=0x0008;
- public static final int MASK_KEY_DOWN_FLAG=0x0800;
- public static final int MASK_KEY_OK=0x0010;
- public static final int MASK_KEY_OK_FLAG=0x1000;
- }