Thread: Color Game

    #16
  1. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    need an infinite loop that changes the current colour and mouselisteners for each round.
    The "loop" in GUI programming is between the user and the listener code.
    The user causes an event, the code reacts,
    The user causes an event, the code reacts,
    The user causes an event, the code reacts,
    forever until the user closes the program
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    And how would I implement such a loop into my program?
  4. #18
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You don't implement such a loop. It exists with GUI code using events.

    Here is the loop:
    Program sets up GUI
    begin loop
    the jvm waits for a user event
    The user causes an event
    the code reacts to the event
    end loop

    forever until the user closes the program
    Last edited by NormR; March 6th, 2013 at 04:42 PM.
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Ok, so baring that in mind, I added a function called playGame()

    Code:
    void playGame() {
            for( int i=0; i<rArray.length; i++ ) {
                System.out.println("array col: " + rArray[i].col());
                if( rArray[i].col() == prev ) {
                    System.out.println("listener: " + rArray[i].col());
                    rArray[i].addMouseListener(rCurrent);
                    prev = rCurrent.col();
                    System.out.println("ani prev:" + prev);
                }
            }
        }
    The output only includes each println statement once. If there is this loop as you say, shouldn't the program output statements every time a mouse event occurs?
  8. #20
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    This statement:
    Code:
    System.out.println("array col: " + rArray[i].col());
    should print if the array is not empty

    The next println() statement requires this if statement to be true:
    Code:
     if( rArray[i].col() == prev )
    When is it ever true? Add a println() statement just before the if that prints the values of the variables used in the if so you can see what they are and know when the if should be true.

    every time a mouse event occurs?
    Listeners are only called when they have been added to the component that can receive the event. Has that been done?
    Last edited by NormR; March 7th, 2013 at 12:52 PM.
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    The next println() statement requires this if statement to be true:
    Code:
     if( rArray[i].col() == prev )
    When is it ever true? Add a println() statement just before the if that prints the values of the variables used in the if so you can see what they are and know when the if should be true.
    Well it is certainly true initially. The output I recieve is:
    Code:
    array col: java.awt.Color[r=255,g=0,b=0]
    array col: java.awt.Color[r=0,g=255,b=0]
    array col: java.awt.Color[r=0,g=0,b=255]
    listener: java.awt.Color[r=0,g=0,b=255]
    ani prev:java.awt.Color[r=255,g=0,b=0]
    array col: java.awt.Color[r=255,g=255,b=0]
    array col: java.awt.Color[r=255,g=175,b=175]
    array col: java.awt.Color[r=128,g=0,b=128]
    array col: java.awt.Color[r=255,g=128,b=0]
    array col: java.awt.Color[r=202,g=225,b=255]
    NOTE: I have changed prev's initial colour to blue since I last posted.

    That's all that is outputted.

    Originally Posted by NormR
    Listeners are only called when they have been added to the component that can receive the event. Has that been done?
    Isn't that part of my for loop?:
    Code:
    rArray[i].addMouseListener(rCurrent);
    I changed the method to this (changed == to .equals() and added a new println):
    Code:
    void playGame() {
            for( int i=0; i<rArray.length; i++ ) {
                System.out.println("array col: " + rArray[i].col());
                if( rArray[i].col().equals(prev) ) {
                    System.out.println("listener: " + rArray[i].col());
                    rArray[i].addMouseListener(rCurrent);
                    prev = rCurrent.col();
                    System.out.println("ani prev:" + prev);
                }
            }
            System.out.println("Current:" + rCurrent.col());
        }
    The system outputs the last println statement once, even when rCurrent changes.
  12. #22
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You should print the value of prev BEFORE the if test so you can see what the computer sees when it executes the if
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    I've now done that, and I prints what I expected. It doesn't change the fact that these statements don't reprint when I click something.
  16. #24
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I can not tell what the code is doing by only looking at one method. You will have to post the whole program so it can be tested.

    The 2nd and 3rd print statements will not execute and print if this if statement is false:
    Code:
    if( rArray[i].col().equals(prev) ) {
    Why is it false?

    when I click something.
    Does the something have a listener? Is the listener called when the something is clicked? Does the println in the listener print out a message so you know it is called?
  18. #25
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    The 2nd and 3rd print statements will not execute and print if this if statement is false:
    Code:
    if( rArray[i].col().equals(prev) ) {
    Why is it false?
    They print in one instance, when the array is the same color as prev. Which is correct. But shouldn't this reprint as the game continues?

    Originally Posted by NormR
    Does the something have a listener? Is the listener called when the something is clicked? Does the println in the listener print out a message so you know it is called?
    I added a println() statement to the listener. It is called when the one square is clicked.

    Originally Posted by NormR
    I can not tell what the code is doing by only looking at one method. You will have to post the whole program so it can be tested.
    Here is the whole program.
    NOTES:
    - To start, click the blue square.
    - Problem: Only the blue square calls the listener for the entirety, this should only be the case for round 1

    Game.java
    Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    
    class Game implements Runnable
    {
        private Rect[] rArray = new Rect[8];
        private cRect rCurrent = new cRect(Color.red);
        private Color prev = Color.blue;
    
        public static void main(String[] args)
        {
            Game program = new Game();
            program.colours();
            SwingUtilities.invokeLater(program);
            System.out.println("Prev: " + program.prev);
            program.playGame();
        }
    
        // Create the main window for the program.
        public void run()
        {
            JFrame w = new JFrame();
            w.setDefaultCloseOperation(w.EXIT_ON_CLOSE);
            w.setTitle("Color Game");
            w.setSize(600, 400);
            w.add(display());
            w.setLocationByPlatform(true);
            w.setVisible(true);
        }
        
        void playGame() {
                for( int i=0; i<rArray.length; i++ ) {
                    rArray[i].removeMouseListener(rCurrent);
                    System.out.println("array col: " + rArray[i].col());
                    System.out.println("prev before if:" + prev);
                    if( rArray[i].col().equals(prev) ) {
                        System.out.println("listener: " + rArray[i].col());
                        rArray[i].addMouseListener(rCurrent);
                        prev = rCurrent.col();
                        System.out.println("new prev:" + prev);
                        break;
                    }
                }
                System.out.println("Current:" + rCurrent.col());
        }
        
        void colours(){
            rArray[0] = new Rect(Color.red);
            rArray[1] = new Rect(Color.green);
            rArray[2] = new Rect(Color.blue);
            rArray[3] = new Rect(Color.yellow);
            rArray[4] = new Rect(Color.pink);
            rArray[5] = new Rect(new Color(128, 0, 128));
            rArray[6] = new Rect(new Color(255, 128, 0));
            rArray[7] = new Rect(new Color(202, 225, 255));
        }
    
        Box display()
        {
            GridLayout grid = new GridLayout(2, 4, 0, 0);
            Border border1 = BorderFactory.createEmptyBorder(20, 20, 50, 20);
            Border border2 = BorderFactory.createEmptyBorder(0, 0, 5, 0);
            JPanel current = new JPanel();
            Box boxcurr = Box.createVerticalBox();
            JLabel label = new JLabel("Current Colour:");
            current.setAlignmentX(JComponent.CENTER_ALIGNMENT);
            label.setBorder(border2);
            boxcurr.add(label);
            boxcurr.add(rCurrent);
            boxcurr.setBorder(border1);
            current.add(boxcurr);
            Box box = Box.createVerticalBox();
            JPanel colArr = new JPanel();
            colArr.setPreferredSize(new Dimension(500, 200));
            colArr.setLayout(grid);
            System.out.println("prev (display): " + prev);
            JLabel select = new JLabel("Select Previous Colour:");
            for( int i=0; i<8; i++ ) {
                colArr.add(rArray[i]);
            }
            box.add(current);
            box.add(select);
            box.add(colArr);
            return box;
        }
    }
    Rect.java
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    class Rect extends JPanel {
    
        private Random gen = new Random();
        private Color[] cols = new Color[8];
        private Color color = new Color(0, 0, 0);
        
        Rect() {
            setPreferredSize(new Dimension(75, 75));
        }
        
        Rect(Color pCol){
            setPreferredSize(new Dimension(75, 75));
            color = pCol;
        }
       
        Color col(){
            return color;
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(color);
            g2.fillRect(0, 0, 75, 75);
        }
    }
    cRect.java
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    class cRect extends JPanel implements MouseListener {
    
        private Random gen = new Random();
        private Color[] cols = new Color[8];
        private Color color = new Color(0, 0, 0);
        
        cRect() {
            setPreferredSize(new Dimension(75, 75));
            setupcols();
        }
        
        cRect(Color pCol){
            setPreferredSize(new Dimension(75, 75));
            color = pCol;
            setupcols();
        }
        
        void setupcols(){
            cols[0] = Color.red;
            cols[1] = Color.green;
            cols[2] = Color.blue;
            cols[3] = Color.yellow;
            cols[4] = Color.pink;
            cols[5] = new Color(128, 0, 128);
            cols[6] = new Color(255, 128, 0);
            cols[7] = new Color(202, 225, 255);
        }
        
        Color col(){
            return color;
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(color);
            g2.fillRect(0, 0, 75, 75);
        }
        
        public void mouseClicked(MouseEvent evt){
            int r = gen.nextInt(8);
            color = cols[r];
            System.out.println("Listener called.");
            repaint();
        }
        
        public void mouseMoved(MouseEvent evt){}
        public void mouseEntered(MouseEvent evt){}
        public void mouseExited(MouseEvent evt){}
        public void mousePressed(MouseEvent evt){}
        public void mouseReleased(MouseEvent evt){}
    }
  20. #26
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Problem: Only the blue square calls the listener for the entirety
    Why is that? Should the other squares have a listener?
  22. #27
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Well for the playGame() method:

    Code:
    void playGame() {
                for( int i=0; i<rArray.length; i++ ) {
                    rArray[i].removeMouseListener(rCurrent);
                    System.out.println("array col: " + rArray[i].col());
                    System.out.println("prev before if:" + prev);
                    if( rArray[i].col().equals(prev) ) {
                        System.out.println("listener: " + rArray[i].col());
                        rArray[i].addMouseListener(rCurrent);
                        prev = rCurrent.col();
                        System.out.println("new prev:" + prev);
                        break;
                    }
                }
                System.out.println("Current:" + rCurrent.col());
        }
    Which I assume is in an infinite loop, the index of the array that has adds a mouselistener should be updated when prev updates.
  24. #28
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I assume is in an infinite loop
    That code is only executed once.
    Look at the debug print out and you will see that.
    The only code that is repeatedly called is the method that prints: Listener called.



    Suggestion: use the mousePressed method instead of the mouseClicked method to make user interactions easier.
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    That code is only executed once.
    Look at the debug print out and you will see that.
    The only code that is repeatedly called is the method that prints: Listener called.



    Suggestion: use the mousePressed method instead of the mouseClicked method to make user interactions easier.
    So how do I make it so the other code is executed repeatedly?
  28. #30
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    make it so the other code is executed repeatedly?
    When do you want the "other code" executed?
    Do you want to use a Timer that will call the "other code" every so many seconds?
    Do you want to execute the "other code" every time a user clicks on a square?
    Or when?

    What does the "other code" do?

IMN logo majestic logo threadwatch logo seochat tools logo