2012-07-01 37 views
0

我试图做一个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(); 
       }    
      } 
     } 
} 
+0

1)为了更好地帮助越早,张贴[SSCCE](http://sscce.org/)。 2)请为类使用明智的名称,即使在测试代码中也是如此。像“SimonGameTest”这样的东西可能适合这个。 3)请学习常见的[Java命名约定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)(具体用于名称的情况) ,方法和属性名称并一致使用。 4)请为代码块使用一致的逻辑缩进。 –

回答

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. 
0

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

+1

这会冻结应用程序1秒钟。 – Ishtar

+0

@Ishtar不,从播放器调用'Play()'方法不是EDT :) – GETah

+1

这是一个'swing.Timer',运行在EDT中,所以它会阻止EDT。 – Ishtar

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,或将其存储在其他地方。