【问题标题】:Scrolling text effect on 2D array二维数组上的滚动文本效果
【发布时间】:2020-09-25 20:44:37
【问题描述】:

对于我正在进行的一个项目,我想成为 abbe 来“滚动”一个数组,如下所示:

这是我目前的代码:

private boolean[][] display = this.parseTo8BitMatrix("Here");
private int scroll = 0;

public void scroll() {
    this.scroll++;
}


//sets the clock's display
public void setDisplay(String s) {
    this.display = this.parseTo8BitMatrix(s);
}

//determines the current frame of the clock
private boolean[][] currentFrame() {
    boolean[][] currentFrame = new boolean[8][32];
    int length = this.display[0].length;
    if(length == 32) { //do nothing
        currentFrame =  this.display;
    } else if(length <= 24) { //center
        for(int i = 0; i < 8; i++) {
            for(int j = 0; j < length; j++) {
                currentFrame[i][j+((32-length)/2)] = this.display[i][j];
            }
        }
        this.display = currentFrame; //set display to currentFrame so the display doesn't get centered each time
    } else { //scroll
        for(int i = 0; i < 8; i++) {
            for(int j = 0; j < length; j++) {
                if(this.scroll+j <= 32) {
                    currentFrame[i][j] = this.display[i][j+this.scroll];
                } else {
                    //?
                }
            }
        }
    }
    return currentFrame;
}

我的代码一直有效,直到数组需要“环绕”到另一端。我哪里出错了?

【问题讨论】:

    标签: java arrays clock


    【解决方案1】:

    我假设您正在寻找一个适用于 else 的公式。 通常模数对于环绕非常有帮助。 你要找的基本上是

    currentFrame[i][j]= this.display[i][(j+this.scroll)%length];
    

    即使没有包裹也可以使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2011-11-04
      • 2014-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多