j2me做的小球按键只会加速不会减速
代码是这样的:import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.midlet.MIDlet;
public class AnimationMIDlet extends MIDlet
implements CommandListener, ItemStateListener {
// MIDlet的显示对象
private Display display;
// 第一个startApp调用的标志
protected boolean started;
// Exit命令
private Command exitCommand;
// Setup命令
private Command setupCommand;
// Run 命令
private Command runCommand;
// 配置form
private Form form;
// 动画画布
private AnimationCanvas canvas;
// 块数量测量
private Gauge blockGauge;
// 帧率测量
private Gauge rateGauge;
// 初始化帧率
private static final int FRAME_RATE = 1;
// 初始化块数量
private static final int BLOCK_COUNT = 1;
protected void startApp() {
if (!started) {
display = Display.getDisplay(this);
form = new Form("Animation");
rateGauge = new Gauge("Frame rate", true, 10, FRAME_RATE);
blockGauge = new Gauge("Blocks", true, 4, BLOCK_COUNT);
form.append(blockGauge);
form.setItemStateListener(this);
canvas = createAnimationCanvas();
exitCommand = new Command("Exit", Command.EXIT, 0);
setupCommand = new Command("Setup", Command.SCREEN, 0);
runCommand = new Command("Run", Command.SCREEN, 0);
canvas.addCommand(exitCommand);
canvas.addCommand(setupCommand);
form.addCommand(exitCommand);
form.addCommand(runCommand);
form.setCommandListener(this);
canvas.setCommandListener(this);
display.setCurrent(form);
started = true;
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
// 退出,不需要调用destroyApp,应为它是空
notifyDestroyed();
} else if (c == runCommand) {
display.setCurrent(canvas);
} else if (c == setupCommand) {
display.setCurrent(form);
}
}
public void itemStateChanged(Item item) {
if (item == blockGauge) {
int count = blockGauge.getValue();
if (count < 1) {
count = 1;
}
canvas.setBlockCount(count);
} else if (item == rateGauge) {
int count = rateGauge.getValue();
if (count < 1) {
count = 1;
}
canvas.setFrameRate(count);
}
}
// 创建画块的画布
protected AnimationCanvas createAnimationCanvas() {
return new AnimationCanvas();
}
class AnimationCanvas extends Canvas {
// 每个块的大小
protected int SIZE= 4;
//小球坐标
int ballX, ballY;
// 沿X方向移动的初始速度
protected final int[] xSpeeds = { 2, -2, 0, -2 };
//沿Y方向移动的初始速度
protected final int[] ySpeeds = { 2, -2, 2, -0 };
// 背景色
protected int background = display.isColor() ? 0 : 0xc0c0c0;
// 前景色
protected int foreground = display.isColor() ? 0xffff00 : 0;
// 屏幕宽度
protected int width = getWidth();
// 屏幕高度
protected int height = getHeight();
// 屏幕修改的速率
protected int frameRate;
// 定义画在屏幕上的块
protected Block[] blocks;
// 定义修改定时器
protected Timer timer;
// 定义修改定时器任务
protected TimerTask updateTask;
// 获得最大块数
public int getMaxBlocks() {
return blocks.length;
}
// 创建缺省设置的画布
AnimationCanvas() {
setBlockCount(BLOCK_COUNT);
setFrameRate(FRAME_RATE);
}
public void keyPressed(int code){
if(code==KEY_NUM4){
frameRate=frameRate+1;
setFrameRate(frameRate);
System.out.println(frameRate);
}
if(code==KEY_NUM5){
frameRate=frameRate-1;
setFrameRate(frameRate);
System.out.println(frameRate);
}
if(code==KEY_NUM6){
SIZE=SIZE+1;
setSize(SIZE);
System.out.println(SIZE);
}
}
// 设置须画的块数
public void setBlockCount(int count) {
if (count > xSpeeds.length) {
throw new IllegalArgumentException("Cannot have more than "
+ xSpeeds.length + " blocks");
}
blocks = new Block[count];
createBlocks();
}
// 获取须画的块数
public int getBlockCount() {
return blocks.length;
}
// 设置每秒须修改数
public void setFrameRate(int frameRate) {
if (frameRate < 1 || frameRate > 10) {
throw new IllegalArgumentException("Frame rate must be > 0 and <= 10");
}
this.frameRate = frameRate;
if (isShown()) {
startFrameTimer();
}
}
public void setSize(int Size){
this.SIZE=Size;
}
// 获得每秒的修改数
public int getFrameRate() {
return frameRate;
}
// Paint画布背景和所有位置上的块
protected void paint(Graphics g) {
// Paint背景色
g.setColor(background);
g.fillRect(0, 0, width, height);
// 画所有的块
g.setColor(foreground);
synchronized (this) {
for (int i = 0, count = blocks.length; i < count; i++) {
g.fillArc(blocks[i].x, blocks[i].y, SIZE, SIZE, 0, 360);
}
}
}
// 通知画布可见
protected void showNotify() {
// 启动帧定时器
startFrameTimer();
}
// 通知画布不再可见
protected void hideNotify() {
// 停止帧定时器
stopFrameTimer();
}
// 创建显示的块
private void createBlocks() {
int startX = (width - SIZE)/2;
int startY = (height - SIZE)/2;
for (int i = 0, count = blocks.length; i < count; i++) {
blocks[i] = new Block(startX, startY, xSpeeds[i], ySpeeds[i]);
}
}
// 启动帧重画定时器
protected void startFrameTimer() {
timer = new Timer();
updateTask = new TimerTask() {
public void run() {
moveAllBlocks();
}
};
long interval = 1000/frameRate;
timer.schedule(updateTask, interval, interval);
}
// 停止帧重画定时器
protected void stopFrameTimer() {
timer.cancel();
}
// 调用定时器终结
public synchronized void moveAllBlocks() {
// 修改所有块的位置和速度
for (int i = 0, count = blocks.length; i < count; i++) {
blocks[i].move();
// 请求屏幕repaint
repaint();
}
}
// 定义代表屏幕上块的内嵌类
class Block {
int x; // X位置
int y; // Y 位置
int xSpeed; // X方向的速度
int ySpeed; // Y方向的速度
Block(int x, int y, int xSpeed, int ySpeed) {
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
}
void move() {
x += xSpeed;
if (x <= 0 || x + SIZE >= width) {
xSpeed = -xSpeed;
}
y += ySpeed;
if (y <= 0 || y + SIZE >= height) {
ySpeed = -ySpeed;
}
}
}
}
}
现在的问题是我按4的话会不断加速,但是按5的话速度就不会减下来。怎么办 --------------------编程问答--------------------
补充:Java , J2ME