【发布时间】:2021-03-14 02:53:47
【问题描述】:
我有一个paintComponent 方法,在一个类中。它生成一个10*10 的网格。 而且我想降低帧率,这样每次函数给网格中的一个矩形上色时,我都能看到进度
public void paint(Graphics g1) {
super.paint(g1);
Graphics2D g= (Graphics2D) g1;
for(Object a: Maze_Generator.list) {
Cell c =(Cell)a;
if(c.top())
g.drawLine(c.x(), c.y(), c.x()+c.length(), c.y());
if(c.bottom())
g.drawLine(c.x(), c.y()+c.length(),c.x()+c.length(),c.y()+c.length());
if(c.left())
g.drawLine(c.x(), c.y(), c.x(), c.y()+c.length());
if(c.right())
g.drawLine(c.x()+c.length(), c.y(), c.x()+c.length(), c.y()+c.length());
// I wish to delay the following code by a second, so that I can see as the square gets coloured one by one.
if(c.visited()) {
g.setColor(Color.cyan);
g.fillRect(c.x()+1, c.y()+1, c.length()-1, c.length()-1);
g.setColor(Color.black);
}
}
我尝试使用 Thread.sleep(),但由于某些原因,应用程序冻结,UI 崩溃(我只看到 JFrame,白色背景,它没有关闭) 但程序仍然在后台运行
try{Thread.sleep(2000);}catch(Exception e){ e.printStackTrace();}
if(c.visited()) {
g.setColor(Color.cyan);
g.fillRect(c.x()+1, c.y()+1, c.length()-1, c.length()-1);
g.setColor(Color.black);
有什么建议吗?
【问题讨论】:
标签: java timer delay java-threads timedelay