我目前正在研究移動背景的2D射擊遊戲。背景只是一個簡單的圖像。我想實現圖像從上到下的無止境的移動。如何實現無限的圖像循環?
我想過要確定位於框架可見部分外部底部的圖像部分,並再次將其繪製在頂部。
@Override
public void paint(Graphics go) {
Graphics2D g = (Graphics2D) go;
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(this.imgBackground, 0, this.yBackground, this);
}
yBackground是繪製圖像左上角的座標。它是從另一個線程改變:
while (!this.paused) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
int y = this.display.getBackgroundY();
if (y == this.display.getHeight()) {
y = 0;
} else {
y++;
}
this.display.setBackgroundY(y);
}
因此,這裏是我的問題:我怎麼能得到這個圖像的部分是幀的可見部分之外?
1.請爲什麼??? 2.不要使用public void paint(Graphics go){,搜索paintComponent,3.不要使用Thread.sleep(10)阻止EDT;使用Swing Timer代替4. sleep(10);是非常快速的暫停,觸及本機操作系統的延遲,從沒有自己的RepaintManager等開始=> 33,爲了更快地發佈[SSCCE](http://sscce.org/),更好的幫助,短的,可運行的,可編譯的 – mKorbel
好的我會記住這一點 – Markus