Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #16  
Old March 6th, 2013, 03:30 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
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

Reply With Quote
  #17  
Old March 6th, 2013, 03:34 PM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
And how would I implement such a loop into my program?

Reply With Quote
  #18  
Old March 6th, 2013, 03:39 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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 03:42 PM.

Reply With Quote
  #19  
Old March 7th, 2013, 11:41 AM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
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?

Reply With Quote
  #20  
Old March 7th, 2013, 11:50 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Quote:
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 11:52 AM.

Reply With Quote
  #21  
Old March 7th, 2013, 12:09 PM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
Quote:
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.

Quote:
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.

Reply With Quote
  #22  
Old March 7th, 2013, 12:48 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
You should print the value of prev BEFORE the if test so you can see what the computer sees when it executes the if

Reply With Quote
  #23  
Old March 7th, 2013, 02:09 PM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
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.

Reply With Quote
  #24  
Old March 7th, 2013, 02:14 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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?

Quote:
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?

Reply With Quote
  #25  
Old March 7th, 2013, 02:59 PM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
Quote:
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?

Quote:
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.

Quote:
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){}
}

Reply With Quote
  #26  
Old March 7th, 2013, 03:20 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
Problem: Only the blue square calls the listener for the entirety

Why is that? Should the other squares have a listener?

Reply With Quote
  #27  
Old March 7th, 2013, 03:26 PM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
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.

Reply With Quote
  #28  
Old March 7th, 2013, 03:36 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
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.

Reply With Quote
  #29  
Old March 7th, 2013, 05:58 PM
EffX EffX is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 38 EffX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
Quote:
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?

Reply With Quote
  #30  
Old March 7th, 2013, 06:05 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Color Game

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap