Вот мой код:Как остановить отображение мерцания при рисовании?
public class Game extends JComponent implements Runnable {
World w = null;
Keyboard keyboard = null;
Thread game = null;
/** The constructor. I would like to initialize here */
public Game(){
// Create and set up the frame
JFrame frame = new JFrame("My Game");
frame.setSize(500, 700);
frame.setLocationRelativeTo(null);
frame.add(this);
frame.setVisible(true);
world = new World();
keyboard = new KeyBoard(this);
game = new Thread(this);
game.start();
}
/** The run() method. I'll be using it as a game loop */
@Override
public void run(){
while(true){
repaint();
try {
Thread.sleep(30);
} catch (InterruptedException ex) {
// I don't want to do anything
}
}
}
/** I was doing a test draw here, but getting an error */
@Override
public void paintComponent(Graphics gr){
Graphics2D g = (Graphics2D) gr;
g.setColor(Color.BLACK);
g.fillRect(200, 200, 200, 200);
}
}
экран часто мерцает. Я попытался изменить значение в вызове sleep()
в методе запуска, но это не решает проблему.
Как остановить мерцание экрана?
Я думаю, что, используя его, вы могли бы сказать, как использовать System.nanoTime() или System.getCurrentTimeMillis() –
(вежливый кашель) - все правильно, конечно, но не лучшее из идей, чтобы повторить неправильный код, IMO :-) – kleopatra
спасибо, чувак, он отлично работал !!!! –