【发布时间】:2014-08-24 22:37:17
【问题描述】:
我在发帖几分钟后想出了一个解决方案,但由于声誉低,我无法删除帖子
为了好玩,我决定开始做一些可能会在某个时候变成游戏的东西。
目前我正在尝试绘制一些圆圈并将它们沿给定方向移动。这会导致闪烁。我很可能监督了一些非常基本的事情,但我不知道为什么它渲染不流畅。
我的董事会课程看起来像(删除了我认为不必要的内容):
public class Board extends Canvas implements Runnable {
public static void main(String[] args) {
Board board = new Board();
board.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame("Circles");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(board);
frame.pack();
frame.setVisible(true);
board.start();
}
@Override
public void run() {
while (running) {
process();
repaint();
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
绘制方法:
public void paint(Graphics g1) {
super.paint(g1);
Graphics2D g = (Graphics2D) g1;
for (Ball ball : Ball.BALLS) {
g.drawOval((int) ball.getLocation().getX(), (int) ball.getLocation().getY(), ball.getRadius(), ball.getRadius());
}
}
我的处理方法:
private void process() {
if (utilities.randInt(1, 100) < 10 && Ball.getBallCount() < 40) {
Ball.spawnNew(this);
}
for (Ball ball : Ball.BALLS) {
ball.move(this);
}
Ball.BALLS.removeAll(Ball.TO_REMOVE);
Ball.TO_REMOVE.clear();
}
move 方法基本上每次调用时都会将球的 x 值增加一个给定值(向右移动)。
就像我说的,我不确定它为什么会闪烁,所以如果您有任何指示,请告诉。
谢谢!
【问题讨论】: