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 4 -
Color Game
Page 4 - 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 9th, 2013, 12:08 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 You could remove all the components on the JFrame and rebuild its contents. |
How can I do that considering I'm inside the cRect class?
Quote: | Originally Posted by MrFujin Before you know how, you should specify when a game is over. |
Here is the updated code. Whenever INCORRECT is printed, that is when the game is over (i.e. whenever an incorrect choice of colour is made).
Code:
Game.java
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.net.*;
class Game implements Runnable {
private Rect[] rArray;
private cRect rCurrent = new cRect(Color.blue);
private boolean gameStarted = true;
public static void main(String[] args)
{
Game program = new Game();
program.colours();
SwingUtilities.invokeLater(program);
program.playGame();
}
void playGame() {
rCurrent.setrCurrent(rCurrent);
rCurrent.playGame(rCurrent);
}
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 colours(){
rArray = rCurrent.getArray();
}
Box display()
{
Box box = Box.createVerticalBox();
if(gameStarted){
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);
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]);
}
box.add(current);
box.add(select);
box.add(colArr);
} else {
box = makeMenu();
}
return box;
}
Box makeMenu(){
Box box = Box.createVerticalBox();
URL u = this.getClass().getResource("logo.png");
ImageIcon image = new ImageIcon(u);
JLabel imageLabel = new JLabel(image);
box.add(imageLabel);
return box;
}
}
Code:
cRect.java
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 color = new Color(0, 0, 0);
private Rect[] rArray = new Rect[8];
private Color prev = Color.red;
private cRect something;
private Rect corRect;
cRect() {
setPreferredSize(new Dimension(75, 75));
setupcols();
}
cRect(Color pCol){
setPreferredSize(new Dimension(75, 75));
color = pCol;
setupcols();
}
void setrCurrent(cRect rCurrent){
something = rCurrent;
}
void playGame(cRect rCurrent) {
int i;
boolean clickDone = false;
for( i=0; i<rArray.length; i++ ) {
rArray[i].removeMouseListener(rCurrent);
System.out.println("removed listener: " + i);
}
for( 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( (clickDone==false) && (rArray[i].col().equals(prev)) ) {
System.out.println("click listener: " + rArray[i].col());
corRect = rArray[i];
rArray[i].addMouseListener(rCurrent);
prev = rCurrent.col();
System.out.println("new prev:" + prev);
clickDone = true;
} else {
rArray[i].addMouseListener(rCurrent);
}
}
System.out.println("Current:" + rCurrent.col());
}
void setupcols(){
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));
corRect = rArray[0];
}
Rect[] getArray(){
return rArray;
}
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 mousePressed(MouseEvent evt){
int r = gen.nextInt(8);
if( evt.getSource() == corRect ) {
System.out.println("CORRECT");
color = rArray[r].col();
playGame(something);
} else {
color = Color.black;
System.out.println("INCORRECT");
}
repaint();
}
public void mouseMoved(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseExited(MouseEvent evt){}
public void mouseClicked(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){}
}
Code:
Rect.java
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);
}
}
|

March 9th, 2013, 12:13 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
What should be shown to the user when the game is over?
Should the mouse listeners stop responding to user actions?
Pass a reference to the cRect class so methods in the cRect class can interact with the class controlling the GUI.
Make cRect an inner class so it has access to the enclosing class's methods.
Last edited by NormR : March 9th, 2013 at 12:17 PM.
|

March 9th, 2013, 02:24 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 What should be shown to the user when the game is over?
Should the mouse listeners stop responding to user actions?
Pass a reference to the cRect class so methods in the cRect class can interact with the class controlling the GUI.
Make cRect an inner class so it has access to the enclosing class's methods. |
What I've done is added a boolean method called gameOver to cRect which returns the value of a variable gameOver, which I update if an incorrect choice is given.
Then in Game, I've added an infinite loop that checks if the game is over. At the moment, it just prints "game running" or "game over" respectively:
Code:
void playGame() {
rCurrent.setrCurrent(rCurrent);
rCurrent.playGame(rCurrent);
while(true){
if( rCurrent.gameOver() ) {
System.out.println("game over");
} else {
System.out.println("game running");
};
}
}
however, if I remove the else section, it never prints game over (even when the game is over):
Code:
void playGame() {
rCurrent.setrCurrent(rCurrent);
rCurrent.playGame(rCurrent);
while(true){
if( rCurrent.gameOver() ) {
System.out.println("game over");
}
}
}
I can't work out why. This is just a test so I can later change the jframe.
Here's the entrie program for reference (Rect remains the same as previous post).
Code:
Game.java
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.net.*;
class Game implements Runnable {
private Rect[] rArray;
private cRect rCurrent = new cRect(Color.blue);
public static void main(String[] args)
{
Game program = new Game();
program.colours();
SwingUtilities.invokeLater(program);
program.playGame();
}
void playGame() {
rCurrent.setrCurrent(rCurrent);
rCurrent.playGame(rCurrent);
while(true){
if( rCurrent.gameOver() ) {
System.out.println("game over");
} else {
System.out.println("game running");
};
}
}
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 colours(){
rArray = rCurrent.getArray();
}
Box display()
{
Box box = Box.createVerticalBox();
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);
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]);
}
box.add(current);
box.add(select);
box.add(colArr);
return box;
}
}
Code:
cRect.java
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 color = new Color(0, 0, 0);
private Rect[] rArray = new Rect[8];
private Color prev = Color.red;
private cRect something;
private Rect corRect;
private boolean gameOver = false;
cRect() {
setPreferredSize(new Dimension(75, 75));
setupcols();
}
cRect(Color pCol){
setPreferredSize(new Dimension(75, 75));
color = pCol;
setupcols();
}
void setrCurrent(cRect rCurrent){
something = rCurrent;
}
void playGame(cRect rCurrent) {
int i;
boolean clickDone = false;
for( i=0; i<rArray.length; i++ ) {
rArray[i].removeMouseListener(rCurrent);
System.out.println("removed listener: " + i);
}
for( 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( (clickDone==false) && (rArray[i].col().equals(prev)) ) {
System.out.println("click listener: " + rArray[i].col());
corRect = rArray[i];
rArray[i].addMouseListener(rCurrent);
prev = rCurrent.col();
System.out.println("new prev:" + prev);
clickDone = true;
} else {
rArray[i].addMouseListener(rCurrent);
}
}
System.out.println("Current:" + rCurrent.col());
}
void setupcols(){
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));
corRect = rArray[0];
}
Rect[] getArray(){
return rArray;
}
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);
}
boolean gameOver(){
return gameOver;
}
public void mousePressed(MouseEvent evt){
int r = gen.nextInt(8);
if( evt.getSource() == corRect ) {
System.out.println("CORRECT");
color = rArray[r].col();
playGame(something);
} else {
color = Color.black;
gameOver = true;
System.out.println("INCORRECT");
}
repaint();
}
public void mouseMoved(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseExited(MouseEvent evt){}
public void mouseClicked(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){}
}
|

March 9th, 2013, 02:37 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
A forever loop is very rarely the way to code a program.
The method that detects the end of the game should call a method in the controlling class and allow that method to react.
|

March 10th, 2013, 08:38 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 38
Time spent in forums: 7 h 16 m 19 sec
Reputation Power: 1
|
|
Ok thanks, now that I've done that however, I'm adding a restart button.
Would creating a new thread achieve what I want? I can't quite get it to work:
Code:
void resetGame(){
System.out.println("(re)set game");
Game newGame = new Game();
newGame.createGame();
Thread newThread = new Thread(newGame);
newThread.start();
}
Basically, it functions, but the new game starts in a new JFrame. I imagine this is because calling "new Game()" creates a new JFrame. How can I create a new thread in the same JFrame?
|

March 10th, 2013, 10:02 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Are you trying to have two games running at the same time?
Why not reuse the JFrame that the first game used?
Remove all the old components and add new components and wait for the user to do something. Don't need a new thread.
Last edited by NormR : March 10th, 2013 at 10:12 AM.
|
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
|
|
|
|
|