0

我正在尝试制作西蒙游戏。我正在编写游戏的中途,但我遇到了问题。我希望程序从队列中读取之前在游戏中的所有值,并以正确的顺序闪烁它们的颜色(我选择将它们变为灰色,然后在第二个恢复正常),这是我的问题。如果您查看该方法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

4 回答 4

3

使用基于 Swing 的单Timer发来翻转颜色并调用repaint(). 有关详细信息,请参阅在 Swing 应用程序中使用计时器

于 2012-07-01T11:22:32.777 回答
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();

您可能必须将 temp 设为 final,或将其存储在其他地方。

于 2012-07-01T11:34:36.997 回答
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.
于 2012-07-01T11:09:32.503 回答
0

Thread.sleep(1000)是你需要的。

于 2012-07-01T11:17:58.343 回答