Thread: Color Game

Page 1 of 4 123 ... Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3

    Arrow Color Game


    (This is my first time using graphics in java)

    I'm try to make a game whereby a color is displayed, and the user has to select the color that appeared previously in order to continue (then the color changes). Similar to http://www.sporcle.com/games/yakubu82/colour-match but with a graphical interface.

    At the moment, I have 8 colours laid out, and the user can select any of them to change the 'current' colour. I can't quite work out how to check if the user was correct in their selection.

    Game.java
    Code:
    import javax.swing.*; 
    import javax.swing.border.*; 
    import java.awt.*;  
    
    class Game implements Runnable {     
        private Rect[] rArray = new Rect[8];
        private Rect rCurrent = new Rect();
        private Rect prev; 
    
        public static void main(String[] args)  {
            Game program = new Game(); 
            SwingUtilities.invokeLater(program);  
       }      
    
       public void run()     {  
           colours();         
           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 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 this = new JPanel();    
          this.setPreferredSize(new Dimension(500, 200));           
          this.setLayout(grid);   
          JLabel select = new JLabel("Select Previous Colour:");    
          for( int i=0; i<8; i++ ) {       
               this.add(rArray[i]);      
               rArray[i].addMouseListener(rCurrent);     
           }         
           box.add(current);    
           box.add(select);    
           box.add(hello);   
           return box;  
       }  
    }
    Rect.java
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    
    class Rect extends JPanel implements MouseListener {
    
        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));
            setupcols();
        }
        
        Rect(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];
            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){}
    
    }
    Please ignore the horrible design for now. I'm still getting to grips with things
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,739
    Rep Power
    1959
    It doesn't compile; I get following errors:
    Game.java:51: not a statement
    JPanel this = new JPanel();
    ^
    Game.java:51: ';' expected
    JPanel this = new JPanel();
    ^
    2 errors
    and
    Game.java:61: cannot find symbol
    symbol : variable hello
    location: class Game
    box.add(hello);
    ^
    1 error
    For you information, this refer to the current class. You cannot specify it as a JPanel as you do in the code now.

    Can you describe the logic you have implemented?
    Looks like the function colours in the game class do the same as the function setupcols in the rect class.

    What are you trying to do where?
    Last edited by MrFujin; March 3rd, 2013 at 04:52 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Apologies, the code should be:

    Code:
    import javax.swing.*; 
    import javax.swing.border.*; 
    import java.awt.*;  
    
    class Game implements Runnable {     
        private Rect[] rArray = new Rect[8];
        private Rect rCurrent = new Rect();
        private Rect prev; 
    
        public static void main(String[] args)  {
            Game program = new Game(); 
            SwingUtilities.invokeLater(program);  
       }      
    
       public void run()     {  
           colours();         
           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 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 hello = new JPanel();    
          hello.setPreferredSize(new Dimension(500, 200));           
          hello.setLayout(grid);   
          JLabel select = new JLabel("Select Previous Colour:");    
          for( int i=0; i<8; i++ ) {       
               hello.add(rArray[i]);      
               rArray[i].addMouseListener(rCurrent);     
           }         
           box.add(current);    
           box.add(select);    
           box.add(hello);   
           return box;  
       }  
    }
    In a nutshell the logic:

    - Rect class creates a filled rectangle of a specified colour
    - Rect class repaints the rectangle a random colour using the mouse click listener
    - Game class displays the current colour, changes colour when a rectangle is clicked.

    I'm trying to achieve this but graphically.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you explain how the program is supposed to work?
    At the start the single top square is black.
    If I click on the lower right square what should happen? (sometimes the top square changes color, sometimes nothing happens)
    What color should the top square change to? Is the new color related to the square that was clicked?

    Then what?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    At the moment, the 8 squares with 'select previous colour' do the exact same thing. They make the 'current colour' change to a new random colour in the array.

    While I want the 'current colour' to change to a new random colour, I only want it to do so if the user correctly identifies which colour had appeared previously. This is the part I haven't programmed yet.

    For example: The 'current colour' is red. The user has just correctly clicked on a blue square (the previous 'current colour'), so the 'current colour' updates randomly to green. The user must now click on a red square to continue.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    One thing I'd do to the code is make another class for the top square with the listener code and the array of colors. The class for the bottom squares does not need any of that.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Ok I've done that, I've also added an if statement to the for loop:

    Code:
    for( int i=0; i<8; i++ ) {
                colArr.add(rArray[i]);
                if( rArray[i].col() == prev.col() ) {
                    rArray[i].addMouseListener(rCurrent);
                    prev = new Rect(rCurrent.col());
                }
            }
    Which works for the first instance, but then from there on out it just 'accepts' the same rectangle colour, rather than update what should be 'accepted' as you go.

    It's hard to explain, here's the updated classes so you can compile it and see what I mean.

    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 Rect prev = new Rect(Color.red);
    
        public static void main(String[] args)
        {
            Game program = new Game();
            SwingUtilities.invokeLater(program);
        }
    
        public void run()
        {
            colours();
            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 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);
            JLabel select = new JLabel("Select Previous Colour:");
            for( int i=0; i<8; i++ ) {
                colArr.add(rArray[i]);
                if( rArray[i].col() == prev.col() ) {
                    rArray[i].addMouseListener(rCurrent);
                    prev = new Rect(rCurrent.col());
                }
            }
            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];
            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){}
    
    }
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    it just 'accepts' the same rectangle colour, rather than update what should be 'accepted' as you go.
    Can you describe the events?
    You click at X
    The program does ???
    You click at Y
    the program does ????
    You click at Z
    The program does ??????
    etc
    Then add some comments like:<<< here color should not change
    where the program went wrong.

    How are you debugging the code? I don't see any println statements that would show where the execution flow is going and how the values of the variables are changing as the code executes.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Prev is originally set to Color.red.
    rCurrent is originally set to Color.red.

    1st Instance
    If you click the red square, prev gets updated to rCurrent's colour (Color.red) and rCurrent gets a new random colour (let's say Color.blue).
    If you click any other square, nothing should happen (for now).

    2nd Instance
    If you click the blue square, prev gets updated to rCurrent's colour (Color.blue) and rCurrent gets a new random colour (let's say Color.green).
    If you click any other square (all squares except blue), nothing should happen.

    and so on...

    What is currently happening:

    1st instance functions correctly.

    2nd instance (clicking a blue square should induce the updates)
    If you click the red square, rCurrent gets a new random colour.
    Every other square, nothing happens.

    With respect to debugging, I was unaware you could use println() with graphical interfaces?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I was unaware you could use println()
    What OS are you on? How do you execute the program? On windows you can execute a java program in a command prompt window where the printlns will show.
    You need to add lots of println statements to print messages and values to show you what the code is doing as it executes.

    Your description talks about program variables. I was wondering what the user sees when the program was executed.

    What is "first instance"?

    The only square that causes any changes to the display when clicked on is the one on the top left. Nothing happens when any of the others are clicked on.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    Your description talks about program variables. I was wondering what the user sees when the program was executed.
    The user sees the rCurrent (initially red) and the 8 squares below.

    Originally Posted by NormR
    What is "first instance"?
    It is supposed to be a game. So in each "instance" or "round", the user selects the previous colour that was shown in the "current colour" square. In the first "instance", the previous colour was red (prev's colour is initially red). If they are correct, they proceed to the next round (prev is updated, rCurrent is updated).

    Originally Posted by NormR
    The only square that causes any changes to the display when clicked on is the one on the top left. Nothing happens when any of the others are clicked on.
    This is my problem. On first execution of the game, this is correct. However, for the next round, only the square that contains the 'previous colour' should cause changes (and none of the other squares should).
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    How are you debugging the code to see what it does as it executes?

    How does the game get started?
    The top square shows a color.
    What should the user do now? Does it make any difference which square the user clicks on the first time?
    What should the program do after the user's first click?
    Then what does the user do next?
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    How are you debugging the code to see what it does as it executes?
    Right now? I'm not, but I'll add println() statements asap.

    Originally Posted by NormR
    How does the game get started?
    The top square shows a color.
    What should the user do now? Does it make any difference which square the user clicks on the first time?
    I will make the top square show a random colour. Later, I will add a label for the first round saying "Click red to start". For now however, the top square shows red, and the only way to start is to click red.

    Originally Posted by NormR
    What should the program do after the user's first click?
    The program should:
    Change the colour of the variable "prev" to the colour of the current top square, then,
    Change the colour of the top square (to a random colour).

    Originally Posted by NormR
    Then what does the user do next?
    The user should click the square with the colour equal to that of "prev" (i.e. the colour they saw previously in the top square).

    This process should repeat.
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    You skipped over this question about the user's first click:
    What should the user do now? Does it make any difference which square the user clicks on the first time?
    the only way to start is to click red.
    Why is that? Should the instructions for the game tell the user he must click the red square to start the game?
    Got it:
    Later, I will add a label for the first round saying "Click red to start"
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by NormR
    You skipped over this question about the user's first click:
    What should the user do now? Does it make any difference which square the user clicks on the first time?

    Why is that? Should the instructions for the game tell the user he must click the red square to start the game?
    Got it:
    I'm slightly confused by this message, did you find the answer to your question?

    In short, I think I need an infinite loop that changes the current colour and mouselisteners for each round. I have no idea how to do this.
Page 1 of 4 123 ... Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo