J2ME兑现的滚动条ScrollBar

J2ME实现的滚动条ScrollBar

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="248" height="331" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"> <param name="src" value="/admin/blogs/586731/e:/scrollbar.png"> <embed type="application/x-shockwave-flash" width="248" height="331" src="/admin/blogs/586731/e:/scrollbar.png"></embed></object> 只需要修改其中的count 变量,就可以调整整个菜单中元素的数量。

 

只要大家修改一下,就可以封装成一个滚动条ScroolBar的控件,以后使用的话,无非就是往这个屏幕里添加控件而已,很简单。

 

大家多提宝贵意见啊!

 

看了要回帖啊@@@@@@@

 

 

 

 

public class TestCanvas extends Canvas implements Runnable{
 
 private boolean isColorChange = false,isRun = true,isVibrate = false;
 private int dragBox_Width = 17,dragBox_Height ;
 private int dragBox_X,dragBox_Y;
 private int dragLine_Width = 9,dragLine_Height;
 public int ScreenWidth,ScreenHeight;
 private Thread th = null;
 private int onDragColor = 0x40e0d0,defaultDragColor = 0x696969;
 private int curKey;
 private int vibrateTime = 20;
 private int vendor,offset = 0,bufImage_Y;
 private Image bufImage = null;
 private Graphics bufGraphics = null;
 int count = 64;
 
 TestCanvas(){
  setFullScreenMode(true);
  ScreenWidth = this.getWidth();
  ScreenHeight = this.getHeight();
  bufImage = Image.createImage(ScreenWidth - dragBox_Width - 5 - (dragBox_Width - dragLine_Width)/2,count*30);
  bufGraphics = bufImage.getGraphics();
  
  bufImage_Y = 0;
  dragLine_Height = ScreenHeight - 40;
  double f = (bufImage.getHeight() /dragLine_Height);
  dragBox_Height = (int)(dragLine_Height/f);
  dragBox_X = ScreenWidth - dragBox_Width - 5 - (dragBox_Width - dragLine_Width)/2;
  dragBox_Y = (ScreenHeight - dragLine_Height)/2;
  start();
 }
 
 public void paint(Graphics g){
  g.setColor(0);
  g.fillRect(0, 0, ScreenWidth, ScreenHeight);
  g.setColor(0xc0c0c0);
  g.fillRoundRect(ScreenWidth - dragBox_Width - 5,
    (ScreenHeight - dragLine_Height)/2, dragLine_Width, dragLine_Height, 5, 5);
  g.setColor(isColorChange?onDragColor:defaultDragColor);
  g.fillRoundRect(dragBox_X, dragBox_Y, dragBox_Width, dragBox_Height, 9, 9);
  bufGraphics.setColor(0);
  bufGraphics.fillRect(0,0,bufImage.getWidth(), bufImage.getHeight());
  bufGraphics.setColor(0xffffff);
  for(int i=0;i<count;i++){
   bufGraphics.drawString((i+1)+"", 20, i*30, 0);
  }
  g.drawImage(bufImage, 0, offset, 0);
 }
 
 public void run(){
  while(isRun){
   try{
    repaint();
    serviceRepaints();
    sleep(100L);
   }catch(Exception e){
    
   }
  }
 }
 
 public void start(){
  if(th == null){
   th = new Thread(this);
  }
  th.start();
 }
 
 public static void sleep(long time){
  try{
   Thread.sleep(time);
  }catch(Exception e){
   
  }
 }
 
 public void keyPressed(int key){
  curKey = key;
  switch(curKey){
  case -7:
   MidletTest.instance.exitApp();
   break;
  }
 }
 
 public void keyReleased(int key){
  curKey = 999;
 }
 
// public void pointerPressed(int x, int y) {
//  isColorChange = true;
//  if(isVibrate == false){
//   MidletTest.instance.vibrate(vibrateTime);
//  }  
//  isVibrate = true;
//  vendor = dragBox_Y;
//  dragBox_Y  = y - dragBox_Height/2; 
//  dragBox_Y = (dragBox_Y - (ScreenHeight - dragLine_Height) / 2) <= 0 ? (ScreenHeight - dragLine_Height) / 2 - 1
//    : dragBox_Y >= ScreenHeight - (ScreenHeight - dragLine_Height)
//      / 2 - dragBox_Height ? ScreenHeight
//      - (ScreenHeight - dragLine_Height) / 2 - dragBox_Height
//      : y - dragBox_Height/2;
//  int i = (dragBox_Y - vendor)*(640/200);
//  System.out.println("屏幕需要走的:"+i);
//  if(i>0){
//   offset -= i;
//  }else{
//   offset += i;
//  }
// }

 public void pointerReleased(int x, int y) {
  isColorChange = false; 
  isVibrate = false;
 }

 public void pointerDragged(int x, int y) {
  isColorChange = true;
  if(isVibrate == false){
//   MidletTest.instance.vibrate(vibrateTime);
  }  
  isVibrate = true;
  vendor = dragBox_Y;
  dragBox_Y  = y - dragBox_Height/2; 
  dragBox_Y = (dragBox_Y - (ScreenHeight - dragLine_Height) / 2) <= 0 ? (ScreenHeight - dragLine_Height) / 2 - 1
    : dragBox_Y >= ScreenHeight - (ScreenHeight - dragLine_Height)
      / 2 - dragBox_Height ? ScreenHeight
      - (ScreenHeight - dragLine_Height) / 2 - dragBox_Height
      : y - dragBox_Height/2;
      bufImage.getHeight();
  float f1 = (bufImage.getHeight() - ScreenHeight);
  float f2 = ScreenHeight - 40 - dragBox_Height;
  float f = (f1/f2);
  float i = ((dragBox_Y - vendor)*f);
  if(y<((ScreenHeight - dragLine_Height)/2+(dragBox_Height/2))){
   offset = 0;
  }
  else
   if((y>=((ScreenHeight - dragLine_Height)/2+dragBox_Height/2))&&(y<=(ScreenHeight -(ScreenHeight - dragLine_Height)/2-dragBox_Height/2) )){
   offset -= i;
  }
  else if((y>(ScreenHeight -(ScreenHeight - dragLine_Height)/2-dragBox_Height/2))){
   int w = 0;
   w = -((count*30)-ScreenHeight);
   offset = w;
  }
  
 }

}