【发布时间】:2013-08-03 09:48:07
【问题描述】:
我目前正在开发具有移动背景的 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);是非常快速的暂停,触及 Native OS 的延迟,从 => 33 开始,没有自己的 RepaintManager 等...为了更好的帮助,请尽快发布 SSCCE,简短,可运行,可编译
-
好的,我会记住的
标签: java image awt paint infinite