user1486061
user1486061

Reputation: 35

Delay between two lines of code - Graphics

I'm trying to make a Simon game. I am mid way in programing the game but I got a problem. I want the program to read from a QUEUE all the values that had previously been in the game and turn flash their colors in the right order (I chose to turn them gray and on second later back to normal) and this is my problem. If you look at the method play() you will see the comment I wrote there. How do I do that?

This is my code:

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(); 
                }               
            }   
        }   
}

Upvotes: 0

Views: 3397

Answers (4)

Ishtar
Ishtar

Reputation: 11662

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();

You may have to make temp final, or store it somewhere else.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

Use a single shot Swing based Timer to flip the color and call repaint(). See Using Timers in Swing Applications for details.

Upvotes: 3

GETah
GETah

Reputation: 21419

Thread.sleep(1000) is what you need.

Upvotes: 0

Tomer
Tomer

Reputation: 17930

Try using 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.

Upvotes: 0

Related Questions