向下翻页

来源:互联网 发布:驾校数据 编辑:程序博客网 时间:2024/04/30 13:36

import javax.microedition.lcdui.*;

public class NewPageCanvas extends Canvas {

 /**
  * 显示在屏幕上的内容
  */
 private String[][] content = null;
 
 /**
  * 默认的字体格式
  */
 private Font pfont = Font.getDefaultFont();
 
 /**
  * 默认字体的高度
  */
 private int stringHeight = pfont.getHeight();
 
 /**
  * 屏幕最多可以画多少行
  */
 private int countLine = 0;
 
 private int putIndex = 0;
 
 private int index = 0;
 
 public NewPageCanvas() {
  // TODO Auto-generated constructor stub
  //内容
  content = new String[50][50];
  for(int i = 0; i < 50; i++){
   content[i][i] = "" + (i + 1);
  }
  
  //每屏可以画多少行
  countLine = getHeight()/(stringHeight + 10);
 }
 
 protected void paint(Graphics g) {
  // TODO Auto-generated method stub
  //清屏
  g.setColor(0xffffff);
  g.fillRect(0 , 0, getWidth(), getHeight());
  
  //每行内容的纵坐标位置
  int yWidth = 10;
  
  //设置字体的颜色
  g.setColor(255,0,0);
  
  //把内容画在屏幕上 
  for(;index < content.length; index++){
   g.drawString(content[index][index], 100, yWidth, 0);
   yWidth += (stringHeight + 10);
  }
 }
 
 public void keyPressed(int keyCode){
  int gameAction = getGameAction(keyCode);
  if(gameAction == DOWN){
   putIndex ++;
   if(index > countLine){
    index = putIndex;
   }
  }
  repaint();
 }
}

原创粉丝点击