通过处理按键模拟加速度,刹车等功能

转载 2006年05月19日 14:41:00

import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;

public class gKey {
public int key_UP;
public int key_DOWN;
public int key_LEFT;
public int key_RIGHT;
public int key_A;
public int key_B;
public int key_C;
public int key_D;
public int key_STAR;
public int key_POUND;
public int key_TIMER;
public int movespeed ;
public int leftvalue ;
public int upvalue ;
public int rightvalue ;
public int downvalue ;

public gKey ( int keytime , int mspeed )
{
  key_TIMER = keytime ;
  refreshKey() ;  
  movespeed = mspeed ;
}

  public void refreshKey ()
{
  key_UP = 0 ;
  key_DOWN = 0 ;
  key_LEFT = 0 ;
  key_RIGHT = 0 ;
  key_A = 0 ;
  key_B = 0 ;
  key_C = 0 ;
  key_D = 0 ;
  key_STAR = 0 ;
  key_POUND = 0 ;
}

public void flushKey ()
{
  if ( key_UP > 0 )
   if ( key_UP < key_TIMER ) key_UP += upvalue ;
  if ( key_DOWN > 0 )
   if ( key_DOWN < key_TIMER ) key_DOWN += downvalue ;
  if ( key_LEFT > 0 )
   if ( key_LEFT < key_TIMER ) key_LEFT += leftvalue  ;
  if ( key_RIGHT > 0 )
   if ( key_RIGHT < key_TIMER ) key_RIGHT += rightvalue ;
}

public void keyPressed(int keyCode,Canvas canvas)
{
     switch (canvas.getGameAction(keyCode)) {
     case Canvas.UP:
         key_UP = (key_UP < key_TIMER)?key_UP+movespeed:key_TIMER ;
         upvalue = movespeed ;
         break;
     case Canvas.DOWN:
      key_DOWN = (key_DOWN < key_TIMER )?key_DOWN+movespeed:key_TIMER ;
         upvalue = movespeed ;
         break;
     case Canvas.LEFT:
      key_LEFT = (key_LEFT < key_TIMER )?key_LEFT+movespeed:key_TIMER ;
      leftvalue = movespeed ;
         break;
     case Canvas.RIGHT:
      key_RIGHT = (key_RIGHT < key_TIMER )?key_RIGHT+movespeed:key_TIMER ;
      rightvalue = movespeed ;
         break;
     case Canvas.GAME_A:
      key_A = (key_A < key_TIMER )?key_A+1:key_TIMER ;
         break;
     case Canvas.GAME_B:
      key_B = (key_B < key_TIMER )?key_B+1:key_TIMER ;
         break;
     case Canvas.GAME_C:
      key_C = (key_C < key_TIMER )?key_C+1:key_TIMER ;
         break;
     case Canvas.GAME_D:
      key_D = (key_D < key_TIMER )?key_D+1:key_TIMER ;
         break;
     case 0:
     switch (keyCode) {
      case Canvas.KEY_NUM2:
       key_UP = (key_UP < key_TIMER )?key_UP+movespeed:key_TIMER ;
       upvalue = movespeed ;
          break;
      case Canvas.KEY_NUM8:
       key_DOWN = (key_DOWN < key_TIMER )?key_DOWN+movespeed:key_TIMER ;
          upvalue = movespeed ;
          break;
         case Canvas.KEY_NUM4:
       key_LEFT = (key_LEFT < key_TIMER )?key_LEFT+movespeed:key_TIMER ;
       leftvalue = movespeed ;
          break;
         case Canvas.KEY_NUM6:
       key_RIGHT = (key_RIGHT < key_TIMER )?key_RIGHT+movespeed:key_TIMER ;
       rightvalue = movespeed ;
          break;
      case Canvas.KEY_POUND:
       key_POUND = (key_POUND < key_TIMER )?key_POUND+1:key_TIMER ;
          break;
      case Canvas.KEY_STAR:
       key_STAR = (key_STAR < key_TIMER )?key_STAR+1:key_TIMER ;
          break;
         }
         break;
     }
}

    public void keyReleased(int keyCode,Canvas canvas) {
     switch (canvas.getGameAction(keyCode)) {
     case Canvas.UP:
      upvalue = - (key_UP / 4) ;
         break;
     case Canvas.DOWN:
      downvalue = - (key_DOWN / 4) ;
         break;
     case Canvas.LEFT:
      leftvalue = - (key_LEFT / 4) ;
         break;
     case Canvas.RIGHT:
      rightvalue = -(key_RIGHT / 4) ;
         break;
     case Canvas.GAME_A:
      key_A = 0 ;
         break;
     case Canvas.GAME_B:
      key_B = 0 ;
         break;
     case Canvas.GAME_C:
      key_C = 0 ;
         break;
     case Canvas.GAME_D:
      key_D = 0 ;
         break;
     case 0:
     switch (keyCode) {
      case Canvas.KEY_NUM2:
       upvalue = -(key_UP / 4) ;
          break;
      case Canvas.KEY_NUM8:
       downvalue = -(key_DOWN / 4) ;
          break;
         case Canvas.KEY_NUM4:
       leftvalue = -(key_LEFT / 4) ;
          break;
         case Canvas.KEY_NUM6:
       rightvalue = -(key_RIGHT / 4) ;
          break;
      case Canvas.KEY_POUND:
       key_POUND = 0 ;
          break;
      case Canvas.KEY_STAR:
       key_STAR = 0 ;
          break;
        }
        break;
    }
    }

}

import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;

public class gCanvas extends Canvas implements Runnable {

private Graphics bgGraphics;
private Image bgBuf;
private int width ;
private int height ;
private int dfwidth ;
private int dfheight ;
private int snoffx ;
private int snoffy ;
private boolean isOver;
private boolean isFirstRun ;
public gKey keyParam ;
public int thDelay ;
private Thread mThread ;


// test
private int x,y ;

public gCanvas (int w,int h,int keytimer,int thdelay)
{
  try{
   width = getWidth() ;
   height = getHeight() ;
   dfwidth = w ;
   dfheight = h ;
   isFirstRun = true ;
   snoffx = ( width - dfwidth ) / 2 ;
   snoffy = ( height - dfheight ) / 2 ;
   isOver = false ;
   bgBuf = Image.createImage ( dfwidth, dfheight ) ;
   bgGraphics = bgBuf.getGraphics() ;
   mThread = new Thread(this) ;
   x = 0 ;
   y = 0 ;
   keyParam = new gKey ( keytimer) ;
   thDelay = thdelay ;  
   // test
  }
  catch(Exception ex){
   isOver = true ;
  }
  
}


public void OverProcess ()
{
  isOver = true ;
  // add destroy code
}

public void run ()
{
  try{
   while ( !isOver )
   {
    // add execute code
    if ( keyParam.key_A > 0 ){
     keyParam.refreshKey() ;  // 键按下后只响应一次要调用这个函数
     OverProcess() ;
    }
    
    if ( y > 0 ) y -= keyParam.key_UP ;
    if ( y < width - 32 ) y += keyParam.key_DOWN ;//这里读出来的值不是顾定的,你按的越长值越大,上限是keytime,表示加速度,当你松开键时这里的值不会马上为0,但是会很快变成0,表示惯性
    if ( x > 0 ) x -= keyParam.key_LEFT ;
    if ( x < height - 32 ) x += keyParam.key_RIGHT ;
    keyParam.flushKey() ;
    
    
    repaint() ;
    mThread.sleep(thDelay);
   }
  }
  catch(Exception ex)
  {}
}

public void paint (Graphics g)
{
  // clear screen
  if ( isFirstRun == true ){
   g.setColor(0) ;
   g.fillRect ( 0 , 0 , width , height ) ;
  }
  
  
  // add draw function

  g.drawImage (bgBuf,snoffx,snoffy,g.LEFT|g.TOP) ;
}


    public void keyPressed(int keyCode) {
     keyParam.keyPressed(keyCode,this) ;
    }


    public void keyReleased(int keyCode) {
     keyParam.keyReleased(keyCode,this) ;
    }

}

模拟DVD 实现录入 查看 借出 归还 删除 新增 等功能<分层>

package cn.jbit.yang.qianqian; public class testDVDMgr { public static void main(String[] args) { ...

SWT模拟Eclipse左侧菜单,简单实现新建工程,新建包,添加用例等功能.

package rcpdemo; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; ...
  • kunzhai
  • kunzhai
  • 2012年03月12日 22:52
  • 634

Java swing实现录音、播放、180多种乐器模拟、电子钢琴等功能

大家好,今天给大家介绍一个由Java swing实现的本地音频播放、录音、180多种乐器模拟、电子琴模拟、摇滚谱子制作的一款项目,这个项目说大不大,说小也不小,涉及到比较专业的知识,可能适合搞音乐又爱...
  • llqqxf
  • llqqxf
  • 2017年11月29日 14:53
  • 70

stm32 刹车 功能

stm32 有pwm输出,可以用来驱动电机,刹车就是关掉pwm紧急停止的意思。
  • gtkknd
  • gtkknd
  • 2014年08月27日 14:11
  • 4669

java图片文件处理工具类【包括图片缩放,剪切等功能】

import java.awt.Graphics;  import java.awt.GraphicsConfiguration;  import java.awt.GraphicsDevice;  ...
  • ntotl
  • ntotl
  • 2016年03月15日 00:22
  • 367
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:通过处理按键模拟加速度,刹车等功能
举报原因:
原因补充:

(最多只允许输入30个字)