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

Java 两行代码之间的延迟-图形,java,swing,graphics,repaint,Java,Swing,Graphics,Repaint,我在试着做一个决定。 我正在为游戏编程,但我遇到了一个问题。 我想让程序从一个队列中读取之前在游戏中出现的所有值,并按正确的顺序(我选择将它们变成灰色,然后在第二秒恢复正常)闪烁它们的颜色,这就是我的问题。如果您查看方法play(),您将看到我在那里写的注释。我该怎么做 这是我的代码: import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent;

我在试着做一个决定。 我正在为游戏编程,但我遇到了一个问题。 我想让程序从一个队列中读取之前在游戏中出现的所有值,并按正确的顺序(我选择将它们变成灰色,然后在第二秒恢复正常)闪烁它们的颜色,这就是我的问题。如果您查看方法
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(); 
                }               
            }   
        }   
}
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.awt.geom.Arc2D;
导入java.util.Date;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入unit4.collectionsLib.Queue;
公共类窗口扩展JPanel实现MouseListener、ActionListener{
队列数据=新队列();
Queue temp=new Queue();
int随机;
定时器预启动;
int启动前计数;
Color[]colors={Color.red,Color.blue,Color.yellow,Color.green};
公共窗口(){
prestart=新计时器(1000,此);
int-prestartcount=0;
prestart.start();
}
公共无效播放(){
random=(int)(Math.random()*4);
数据。插入(随机);
int x=0;
色温=颜色。黑色;
x=数据。删除();
该温度插入件(x);
温度=颜色[x];
颜色[x]=颜色为灰色;
//这里延迟一秒钟
颜色[x]=温度;
}   
公共组件(图形g){
超级组件(g);
g、 设置颜色(颜色[0]);
g、 fillArc(80、150、250、250、0、360);
g、 setColor(颜色[1]);
g、 fillArc(80、150、250、250、0、270);
g、 setColor(颜色[2]);
g、 fillArc(80、150、250、250、0、180);
g、 setColor(颜色[3]);
g、 fillArc(80、150、250、250、0、90);
g、 抽绳(启动前计数+“”,0,30);
重新油漆();
}
@凌驾
公共无效鼠标单击(鼠标事件arg0){
arg0.getLocationOnScreen();
}
@凌驾
public void mouseEntered(MouseEvent arg0){
}
@凌驾
public void mouseExited(MouseEvent arg0){
}
@凌驾
public void mousePressed(MouseEvent arg0){
}
@凌驾
公共无效MouseEvent arg0{
}
@凌驾
已执行的公共无效行动(行动事件法){
if(act.getSource()==prestart){
如果(启动前计数尝试使用

尝试使用


是您所需要的。

是您所需要的。

使用单次摆动基础翻转颜色并调用
重新绘制()
。有关详细信息,请参阅。

使用单次摆动基础翻转颜色并调用
重新绘制()
。有关详细信息,请参阅

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最终版本,或者将其存储在其他地方


您可能需要将temp设置为final,或将其存储在其他地方。

这将使应用程序冻结1秒。@Ishtar否,从计时器调用
Play()
方法,而不是从EDT:)这是一个
swing.Timer
,在EDT中运行,因此它将阻止EDT。@Ishtar True,抱歉没有看到这是一个swing Timer,这将冻结应用程序1秒。@Ishtar否,播放()
方法是从计时器而不是EDT中调用的:)它是一个
swing。计时器在EDT中运行,因此它将阻止EDT。@Ishtar True,抱歉没有看到它是一个swing计时器1)为了更快地获得更好的帮助,请发布一个。2)请为类使用一个合理的名称,即使在测试代码中也是如此。类似于
SimonGameTest
的名称可能适用于this.3)请学习通用(特别是用于名称的案例)对于类、方法和属性名称,请一致使用。4)请对代码块使用一致的逻辑缩进。1)为了更快地获得更好的帮助,请发布一篇文章。2)请对类使用合理的名称,即使是在测试代码中。类似于
SimonGameTest
的内容可能适用于此。3)请学习常见代码(特别是用于名称的大小写)对于类、方法和属性名称,请一致使用。4)请对代码块使用一致的逻辑缩进。
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();