我在一个类中有一个 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);
有什么建议么?