两行代码之间的延迟 - 图形

时间:2012-07-01 11:06:10

标签: java swing graphics repaint

我试图制作Simon game。 我在编程游戏的中途,但我遇到了问题。 我希望程序从队列中读取以前在游戏中所有的值,然后按正确的顺序转动它们的颜色(我选择将它们变为灰色,然后再将它们恢复正常)这就是我的问题。如果你看一下方法play(),你会看到我在那里写的评论。我该怎么做?

这是我的代码:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Arc2D;
import java.util.Date;

import javax.swing.JPanel;
import javax.swing.Timer;

import unit4.collectionsLib.Queue;

public class window extends JPanel implements MouseListener , ActionListener{

    Queue <Integer>data = new Queue<Integer> ();
    Queue <Integer>temp = new Queue<Integer> ();
    int random;
    Timer prestart;
    int prestartcount;
    Color [] colors = {Color.red,Color.blue,Color.yellow,Color.green};  

    public window (){       
        prestart = new Timer (1000,this);
        int prestartcount=0;    
        prestart.start();       
    }

    public void play (){            
        random = (int)(Math.random()*4);
        data.insert(random);

        int x=0;
        Color temp=Color.black; 
        x = data.remove();
        this.temp.insert(x);
            temp = colors[x];
        colors[x]=Color.gray;
        // delay of one second here
        colors[x]=temp;
    }   

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(colors[0]);
        g.fillArc(80, 150, 250, 250, 0, 360);

        g.setColor(colors[1]);
        g.fillArc(80, 150, 250, 250, 0, 270);

        g.setColor(colors[2]);
        g.fillArc(80, 150, 250, 250, 0, 180);

        g.setColor(colors[3]);
        g.fillArc(80, 150, 250, 250, 0, 90);        

        g.drawString(prestartcount+"", 0, 30);
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        arg0.getLocationOnScreen();     
    }

    @Override
    public void mouseEntered(MouseEvent arg0) { 
    }


    @Override
    public void mouseExited(MouseEvent arg0) {      
    }


    @Override
    public void mousePressed(MouseEvent arg0) {     
    }


    @Override
    public void mouseReleased(MouseEvent arg0) {
    }


    @Override
    public void actionPerformed(ActionEvent act) {
        if (act.getSource()==prestart){
            if (prestartcount<3)
                prestartcount++;
            else{
                prestart.stop();
                play(); 
                }               
            }   
        }   
}

4 个答案:

答案 0 :(得分:3)

使用单拍Swing Timer来翻转颜色并调用repaint()。有关详细信息,请参阅Using Timers in Swing Applications

答案 1 :(得分:1)

colors[x]=Color.gray;
// delay of one second here
timer = new Timer(0, new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent evt) {
      colors[x]=temp; 
      repaint(); //repaint the gui, or you want see the effect
  }
});
timer.setInitialDelay(1000); //wait one second
timer.setRepeats(false); //only once
timer.start();

您可能需要进行临时决赛,或将其存放在其他地方。

答案 2 :(得分:0)

尝试使用Thread.sleep()

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.

答案 3 :(得分:0)

Thread.sleep(1000)就是您所需要的。