The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Page 2 -
Color Game
Page 2 - Discuss Color Game in the Java Help forum on Dev Shed. Color Game Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 6th, 2013, 03:30 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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
|

March 6th, 2013, 03:34 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
|
|
|
And how would I implement such a loop into my program?
|

March 6th, 2013, 03:39 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

March 7th, 2013, 11:41 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
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?
|

March 7th, 2013, 11:50 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

March 7th, 2013, 12:09 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
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.
|

March 7th, 2013, 12:48 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
You should print the value of prev BEFORE the if test so you can see what the computer sees when it executes the if
|

March 7th, 2013, 02:09 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
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.
|

March 7th, 2013, 02:14 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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?
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?
|

March 7th, 2013, 02:59 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
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){}
}
|

March 7th, 2013, 03:20 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | Problem: Only the blue square calls the listener for the entirety |
Why is that? Should the other squares have a listener?
|

March 7th, 2013, 03:26 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
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.
|

March 7th, 2013, 03:36 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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.
|

March 7th, 2013, 05:58 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
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?
|

March 7th, 2013, 06:05 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|