⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableitem.java

📁 本光盘是《J2ME无线移动游戏开发》一书的配套光盘
💻 JAVA
字号:
package ch04;

import javax.microedition.lcdui.*;

public class TableItem
    extends CustomItem {
  private int rows, cols; //保存表格内行列数
  private int dx, dy; //保存单元格大小
  private int currentX, currentY; //保存当前选中位置
  private String[][] data; //保存各个单元格文字
  private int maxWidth, maxHeight; //保存当前对象要求大小
  private int curWidth, curHeight; //保存当前对象实际大小
  private boolean isShown = false; //保存当前是否处于显示状态
  private boolean isIn = false; //保存当前是否处于焦点状态

  //参数 label:标题
  //c,r:表格的列数和行数
  //w,h: 单元格的宽度和高度
  public TableItem(String label, int c, int r, int w, int h) {
    super(label);
    currentX = currentY = 0;
    rows = r;
    cols = c;
    data = new String[rows][cols];
    dx = w;
    dy = h;
    maxWidth = dx * cols + 1;
    maxHeight = dy * rows + 1;
    isShown = false;
  }

  public void setCell(int w, int h) { //设置指定位置单元格的宽度和高度,同时重新计算对象大小
    dx = w;
    dy = h;
    maxWidth = dx * cols + 1;
    maxHeight = dy * rows + 1;
    invalidate(); //要求
  }

  public int getCurrentX() { //得到当前选中的单元格的X坐标
    return currentX;
  }

  public int getCurrentY() { //得到当前选中的单元格的Y坐标
    return currentY;
  }

  public String getText(int x, int y) { //得到单元格的文字
    if (data[y][x] == null) {
      data[y][x] = "";
    }
    return data[y][x];
  }

  public void setText(String text, int x, int y) { //设置指定位置单元格的文字,同时刷新单元格区域
    data[y][x] = text;
    if (isShown) {
      repaint(x * dx, y * dy, dx, dy);
    }
  }

  //计算尺寸要求的相关重载函数
  protected int getMinContentHeight() {
    System.out.println("getMinContentHeight return " + maxHeight);
    return maxHeight;
  }

  protected int getMinContentWidth() {
    System.out.println("getMinContentWidth return " + maxWidth);
    return maxWidth;
  }

  protected int getPrefContentHeight(int width) {
    System.out.println("getPrefContentHeight return " + maxHeight);
    return maxHeight;
  }

  protected int getPrefContentWidth(int height) {
    System.out.println("getPrefContentWidth return " + maxWidth);
    return maxWidth;
  }

  //尺寸变化时的通知函数
  protected void sizeChanged(int w, int h) {
    System.out.println("sizeChanged (w,h)=(" + w + "," + h + ")");
    curWidth = w;
    curHeight = h;
  }

  //显示与隐藏时的通知函数
  protected void showNotify() {
    System.out.println("showNotify");
    isShown = true;
  }

  protected void hideNotify() {
    System.out.println("hideNotify");
    isShown = false;
  }

  //重绘函数
  protected void paint(Graphics g, int w, int h) {
    System.out.println("paint (w,h) (" + w + "," + h + ")");
    g.setColor(0x00ffffff); //用白色填充整个区域
    g.fillRect(0, 0, w, h);
    g.setColor(0x00000000); //用黑色绘制线条
    for (int i = 0; i <= rows; i++) { //绘制横线
      g.drawLine(0, i * dy, cols * dx, i * dy);
    }
    for (int i = 0; i <= cols; i++) { //绘制竖线
      g.drawLine(i * dx, 0, i * dx, rows * dy);
    }
    g.setColor(0x00D0D0D0); //用灰色填充当前被选中单元格
    g.fillRect( (currentX * dx) + 1, (currentY * dy) + 1, dx - 1, dy - 1);
    g.setColor(0x00000000); //用黑色显示文字
    for (int i = 0; i < rows; i++) { //逐个绘制每个单元格
      for (int j = 0; j < cols; j++) {
        if (data[i][j] != null) {
          g.setClip( (j * dx) + 1, i * dy, dx - 1, dy - 1);
          g.drawString(data[i][j], (j * dx) + 2, ( (i + 1) * dy) - 2,
                       Graphics.BOTTOM | Graphics.LEFT);
        }
      }
    }
  }

  protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
                             int[] visRect_inout) { //处理方向键事件
    System.out.println("viewport (w,h) (" + viewportWidth + "," +
                       viewportHeight + ")");
    System.out.println("visRect_in (" + visRect_inout[0] + "," +
                       visRect_inout[1] + "," + visRect_inout[2] + "," +
                       visRect_inout[3] + ")");
    if (!isIn) { //得到焦点
      isIn = true;
      return true;
    }
    switch (dir) {
      case Canvas.DOWN:
        if (currentY < (rows - 1)) {
          currentY++;
          repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
          repaint(currentX * dx, currentY * dy, dx, dy);
        }
        else {
          return false;
        }
        break;
      case Canvas.UP:
        if (currentY > 0) {
          currentY--;
          repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
          repaint(currentX * dx, currentY * dy, dx, dy);
        }
        else {
          return false;
        }
        break;
      case Canvas.LEFT:
        if (currentX > 0) {
          currentX--;
          repaint( (currentX + 1) * dx, currentY * dy, dx, dy);
          repaint(currentX * dx, currentY * dy, dx, dy);
        }
        break;
      case Canvas.RIGHT:
        if (currentX < (cols - 1)) {
          currentX++;
          repaint( (currentX - 1) * dx, currentY * dy, dx, dy);
          repaint(currentX * dx, currentY * dy, dx, dy);
        }
        break;
    }

    visRect_inout[0] = currentX;
    visRect_inout[1] = currentY;
    visRect_inout[2] = dx;
    visRect_inout[3] = dy;
    System.out.println("visRect_out (" + visRect_inout[0] + "," +
                       visRect_inout[1] + "," + visRect_inout[2] + "," +
                       visRect_inout[3] + ")");

    return true;
  }

  protected void traverseOut() {
    System.out.println("traverseOut");
    isIn = false;
  }

  protected void keyPressed(int keyCode) {
    System.out.println("keyCode=" + keyCode);
    if (data[currentY][currentX] == null) {
      data[currentY][currentX] = "";

    }
    if (keyCode == -8) { //删除最后一个字符
      int len = data[currentY][currentX].length();
      if (len > 0) {
        data[currentY][currentX] = data[currentY][currentX].substring(0,
            len - 1);
      }
    }
    else if ( (char) keyCode >= '0' && (char) keyCode <= '9') {
      data[currentY][currentX] += (char) keyCode; //添加到末尾
    }
    repaint(currentX * dx, currentY * dy, dx, dy); //重绘屏幕
  }

  protected void pointerPressed(int x, int y) {
    System.out.println("pointerPressed (x,y)= (" + x + "," + y + ")");
    if (x >= 0 && x < maxWidth && y >= 0 && y < maxHeight) {
      currentX = x / dx;
      currentY = y / dy;
      repaint(currentX * dx, currentY * dy, dx, dy); //重绘屏幕
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -