Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Delay ..~

  1. #1
    R3.RyozKidz Guest

    Delay ..~

    Here is my code :
    Code:
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.GridLayout; 
     
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.JOptionPane; 
     
    import java.util.Random; 
     
    public class Game extends JPanel 
    { 
        private JFrame frame; 
        private JButton buttons[]; 
        private Random random; 
         
        private String labels[] = { "1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8"}; 
         
        private int toggleButton; 
        private int pairMatched; 
        private int firstEvent; 
        private int secondEvent; 
         
        public Game() 
        { 
            super(new GridLayout(4 , 4 , 5, 5)); 
            frame = new JFrame("Gamex"); 
            buttons = new JButton[16]; 
            random = new Random(); 
             
            toggleButton = 1; 
            pairMatched = 0; 
            firstEvent = 0; 
            secondEvent = 0; 
             
            ActionEventHandler handler = new ActionEventHandler(); 
             
            for(int i = 0 ; i < labels.length ; i++) 
            { 
                int x = random.nextInt(16); 
                String temp = labels[i]; 
                labels[i] = labels[x]; 
                labels[x] = temp; 
                 
                buttons[i] = new JButton("*"); 
                buttons[i].addActionListener(handler); 
                this.add(buttons[i]); 
                 
            } 
             
            frame.setContentPane(this); 
            frame.setSize(400 , 400); 
            frame.setVisible(true); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        } 
         
        private class ActionEventHandler implements ActionListener 
        { 
            public void actionPerformed(ActionEvent event) 
            { 
                if(toggleButton == 1) 
                { 
                    Object source = event.getSource(); 
                     
                    for(int i = 0 ; i < buttons.length ; i++) 
                    { 
                        if(source == buttons[i]) 
                        { 
                            firstEvent = i; 
                        } 
                    } 
                    buttons[firstEvent].setText(labels[firstEvent]); 
                    buttons[firstEvent].setEnabled(false); 
                } 
                 
                if(toggleButton == 2) 
                { 
                    Object source = event.getSource(); 
                     
                    for(int i = 0 ; i < buttons.length ; i++) 
                    { 
                        if(source == buttons[i]) 
                        { 
                            secondEvent = i; 
                        } 
                    } 
                     
                    buttons[secondEvent].setText(labels[secondEvent]); 
                    buttons[secondEvent].setEnabled(false); 
                     
                    toggleButton = 0; 
                     
                    if(labels[firstEvent].equals(labels[secondEvent])) 
                    { 
                        buttons[firstEvent].setEnabled(false); 
                        buttons[secondEvent].setEnabled(false); 
                        pairMatched++; 
                    } 
                    else 
                    { 
                        buttons[firstEvent].setEnabled(true); 
                        buttons[secondEvent].setEnabled(true); 
                        buttons[firstEvent].setText("*"); 
                        buttons[secondEvent].setText("*"); 
                    } 
                     
                    if(pairMatched == 8 ) 
                    { 
                        JOptionPane.showMessageDialog(null , "Congratulation !\nYou have won!" , "Message" , JOptionPane.PLAIN_MESSAGE); 
                    } 
                } 
                 
                toggleButton++; 
            } 
        } 
         
        public static void main(String args[]) 
        { 
            Game xx = new Game(); 
        } 
    }
    I want to use GUI timer to make a delay , but i have tried many times , failed .
    Can anyone help ?

  2. CODECALL Circuit advertisement

     
  3. #2
    abdul.gafur's Avatar
    abdul.gafur is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Salo, Riau, Indonesia
    Posts
    42
    Rep Power
    0

    Re: Delay ..~

    where delay will be inserted and for what?

  4. #3
    R3.RyozKidz Guest

    Re: Delay ..~

    The delay is for the second button .
    I want some kind of pause to make the button to freeze for 1 second so that the user can see what is the text on the button.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Delay ..~

    Why not use sleep(1000)?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    R3.RyozKidz Guest

    Re: Delay ..~

    if i use Thread.sleep(1000) , where am i going to place the exception ? java.lang.InterruptedException ?

  7. #6
    R3.RyozKidz Guest

    Re: Delay ..~

    Here is my code :
    Code:
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.GridLayout; 
     
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.JOptionPane; 
     
    import java.util.Random; 
    import java.lang.InterruptedException; 
     
    public class Game extends JPanel 
    { 
        private JFrame frame; 
        private JButton buttons[]; 
        private Random random; 
         
        private String labels[] = { "1" , "1" , "2" , "2" , "3" , "3" , "4" , "4" , "5" , "5" , "6" , "6" , "7" , "7" , "8" , "8"}; 
         
        private int toggleButton; 
        private int pairMatched; 
        private int firstEvent; 
        private int secondEvent; 
         
        public Game() 
        { 
            super(new GridLayout(4 , 4 , 5, 5)); 
            frame = new JFrame("Gamex"); 
            buttons = new JButton[16]; 
            random = new Random(); 
             
            toggleButton = 1; 
            pairMatched = 0; 
            firstEvent = 0; 
            secondEvent = 0; 
             
            ActionEventHandler handler = new ActionEventHandler(); 
             
            for(int i = 0 ; i < labels.length ; i++) 
            { 
                int x = random.nextInt(16); 
                String temp = labels[i]; 
                labels[i] = labels[x]; 
                labels[x] = temp; 
                 
                buttons[i] = new JButton("*"); 
                buttons[i].addActionListener(handler); 
                this.add(buttons[i]); 
                 
            } 
             
            frame.setContentPane(this); 
            frame.setSize(400 , 400); 
            frame.setVisible(true); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        } 
         
        private class ActionEventHandler implements ActionListener 
        { 
            public void actionPerformed(ActionEvent event) 
            { 
                if(toggleButton == 1) 
                { 
                    Object source = event.getSource(); 
                     
                    for(int i = 0 ; i < buttons.length ; i++) 
                    { 
                        if(source == buttons[i]) 
                        { 
                            firstEvent = i; 
                        } 
                    } 
                    buttons[firstEvent].setText(labels[firstEvent]); 
                    buttons[firstEvent].setEnabled(false); 
                } 
                 
                if(toggleButton == 2) 
                { 
                    Object source = event.getSource(); 
                     
                    for(int i = 0 ; i < buttons.length ; i++) 
                    { 
                        if(source == buttons[i]) 
                        { 
                            secondEvent = i; 
                        } 
                    } 
                     
            try 
            { 
                    buttons[secondEvent].setText(labels[secondEvent]); 
                Thread.sleep(1000); 
                    buttons[secondEvent].setEnabled(false); 
            } 
            catch(InterruptedException e) 
            { 
                System.exit(1); 
            } 
                     
                    toggleButton = 0; 
                     
                    if(labels[firstEvent].equals(labels[secondEvent])) 
                    { 
                        buttons[firstEvent].setEnabled(false); 
                        buttons[secondEvent].setEnabled(false); 
                        pairMatched++; 
                    } 
                    else 
                    { 
     
                        buttons[firstEvent].setEnabled(true); 
                        buttons[secondEvent].setEnabled(true); 
                        buttons[firstEvent].setText("*"); 
                        buttons[secondEvent].setText("*"); 
                    } 
                     
                    if(pairMatched == 8 ) 
                    { 
                        JOptionPane.showMessageDialog(null , "Congratulation !\nYou have won!" , "Message" , JOptionPane.PLAIN_MESSAGE); 
                    } 
                } 
                 
                toggleButton++; 
            } 
        } 
         
        public static void main(String args[]) 
        { 
            Game xx = new Game(); 
        } 
    }
    The Thread.sleep(1000) is working but not what i wanted .
    When the user clicks on the second button , the button doesn't change the text but it does pause for 1 second .
    Still got problem ..~ help !

  8. #7
    abdul.gafur's Avatar
    abdul.gafur is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Salo, Riau, Indonesia
    Posts
    42
    Rep Power
    0

    Re: Delay ..~

    When the user clicks on the second button , the button doesn't change the text but it does pause for 1 second .
    Ya, that is caused that thread.sleep(1000) is run directly when you click on the second button. Although you write it under buttons[secondEvent].setText(labels[secondEvent]).
    So buttons[secondEvent].setText(labels[secondEvent]) is done after sleep(1000).

    That is thread.

  9. #8
    R3.RyozKidz Guest

    Re: Delay ..~

    then any suggestion ? i tried it with others method as well but all ended up with the same thing ..~

  10. #9
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Delay ..~

    You have to put the sleep(1000) call exactly where you want execution to pause. Try moving it around within the method.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  11. #10
    abdul.gafur's Avatar
    abdul.gafur is offline Learning Programmer
    Join Date
    Mar 2010
    Location
    Salo, Riau, Indonesia
    Posts
    42
    Rep Power
    0

    Re: Delay ..~

    But it pause right after we click the second button, before system do buttons[secondEvent].setText(labels[secondEvent]. Although buttons[secondEvent].setText(labels[secondEvent] is written before thread.sleep().

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. delay function in C in number of seconds
    By nerio in forum C and C++
    Replies: 4
    Last Post: 07-23-2011, 10:18 PM
  2. Delay a function execution
    By ferovac in forum JavaScript and CSS
    Replies: 3
    Last Post: 05-27-2011, 08:45 AM
  3. Delay/timer? (other that Sleep();
    By TeenChristian in forum C and C++
    Replies: 72
    Last Post: 07-20-2010, 12:44 PM
  4. how to add in a delay?
    By shifu in forum C and C++
    Replies: 8
    Last Post: 01-29-2010, 02:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts