二维数组滚动文字效果

Scrolling text effect on 2D array

对于我正在从事的项目,我想成为 "scroll" 数组的神父,如下所示:

这是我目前的代码:

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;
}

我的代码一直有效,直到数组需要 "wrap around" 到另一边。我哪里做错了?

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

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

即使没有缠绕也能正常工作。