GameCanvas绘制进度条

//于com.sk.model包创建ProgressCanvas,需要Display对象,以便绘制ProgressCanvas

 package com.sk.model;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

public class ProgressCanvas extends GameCanvas implements Runnable{
 private Display display;
 private int progressBarValue=0;
 private int progressBarX,progressBarY,progressBarWidth,progressBarHeight;
 private boolean sleeping;
 private int frameDelay=33;
 //构造
 public ProgressCanvas(Display display){
  super(true);
  this.display=display;
 }

//开始
 public void start(int x,int y,int width,int height){
  progressBarX=x;
  progressBarY=y;
  progressBarWidth=width;
  progressBarHeight=height;
  display.setCurrent(this);
  new Thread(this).start();
 }
 public void stop(){
  sleeping=true;
 }

//更新 进度
 public void update(){
  int keyState=this.getKeyStates();
  if(keyState == this.RIGHT_PRESSED){
   progressBarValue++;
  }else if(keyState == this.LEFT_PRESSED){
   progressBarValue--;
  }
 }

//绘制进度条
 public void draw(Graphics g){
  //限制 进度条的范围0-100
  if(progressBarValue >=100){
   progressBarValue=100;
  }else if(progressBarValue <=0){
   progressBarValue=0;
  }
  //清屏
  g.setColor(0xFFffffff);
  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  //绘制 圆角外边框
  g.setColor(0x2192bc);
  g.drawRoundRect(progressBarX, progressBarY, progressBarWidth, progressBarHeight, 12, 12);
  //填充 内部
  g.setColor(0x3a7ac9);
  g.fillRoundRect(progressBarX, progressBarY, progressBarValue, progressBarHeight,12,12);
  //绘制 百分比
  g.setColor(0x18adb1);
  g.drawString(progressBarValue+"%", progressBarWidth/2, progressBarHeight/4*3, Graphics.LEFT|Graphics.TOP);
  this.flushGraphics();
 }

//线程
 public void run() {
  // TODO Auto-generated method stub
  Graphics g=this.getGraphics();
  while(!sleeping){
   this.update();
   this.draw(g);
   try {
    Thread.sleep(frameDelay);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 
}
__________________

ProgressCanvas由midlet调用

package com.sk.view;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sk.model.ProgressCanvas;

public class Test extends MIDlet{

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  
 }

 protected void pauseApp() {
  // TODO Auto-generated method stub
  
 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
  ProgressCanvas canvas=new ProgressCanvas(Display.getDisplay(this));
  canvas.start(10,10,100,18);
  
 }

}
//绘制的进度条带圆角,思路是,先绘制出进度条边框,然后不断的填充它

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gamebox1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值