日期:2014-05-20  浏览次数:20858 次

java 模拟led屏显示
谁有这方面的资料,能不能提供一下~~

------解决方案--------------------
有LED的字体,直接使用这种字体显示比较方便。


------解决方案--------------------
Java code
public class Test { 
  public static void main(String[] args) {
    LED led = new LED();
    char[][] chss = led.getLED("0123456789");
    LED.print(chss);
  }
}

class LED {
  /**
  * 每个 LED 的大小,可以进行调整
  */
  public final static int ROW = 7;
  public final static int COL = 7;
  /**
  * 每个 LED 的间隔
  */
  private final static int SEPARATOR = 1;
  private final static char FILL_CHAR = '#';
  private final static char SPACE_CHAR = ' ';

  /**
  * 工具方法,用于输出 LED
  * @param chs
  */
  public static void print(char[][] chs) {
    for (int i = 0; i < chs.length; i++) {
      for (int j = 0; j < chs[i].length; j++) {
        System.out.print(chs[i][j]);
      }
      System.out.println();
    }
  }

  /**
  * 根据数字得到 LED 显示数组
  * @param num *
  * @return
  */
  public char[][] getLED(String num) {
    char[] chs = num.toCharArray();
    char[][][] chsss = new char[chs.length][][];
    for (int i = 0; i < chs.length; i++) {
      chsss[i] = showLed(chs[i] - '0');
    }
    return putManyLed(chsss);
  }

  /**
  * 将多个 LED 组成一排
  * @param chsss
  * @return
  */
  private char[][] putManyLed(char[][][] chsss) {
    if (chsss.length < 1) {
      throw new IllegalArgumentException("LED is NULL!");
    }
    if (chsss.length == 1) {
      return chsss[0];
    }
    char[][] chss = new char[ROW][chsss.length * (COL + SEPARATOR)
        - SEPARATOR];
    for (int i = 0; i < chsss.length; i++) {
      int m = i * (COL + SEPARATOR);
      for (int j = 0; j < chsss[i].length; j++) {
        for (int k = 0; k < chsss[i][j].length; k++) {
          chss[j][m + k] = chsss[i][j][k];
        }
      }
    }
    for (int i = 0; i < chss.length; i++) {
      for (int j = 0; j < chss[i].length; j++) {
        if (chss[i][j] != FILL_CHAR) {
          chss[i][j] = SPACE_CHAR;
        }
      }
    }
    return chss;
  }

  /**
  *
  * @param num
  * @return
  */
  private char[][] showLed(int num) {
    boolean[] b = getLed(num);
    char[][] chs = new char[ROW][COL];
    if (b[0])
      for (int i = 0; i < COL; i++)
        chs[0][i] = FILL_CHAR;
    if (b[1])
      for (int i = 0; i <= ROW / 2; i++)
        chs[i][COL - 1] = FILL_CHAR;
    if (b[2])