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:
  #46  
Old March 9th, 2013, 12:08 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
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);
    }   
}

Reply With Quote
  #47  
Old March 9th, 2013, 12:13 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
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.

Reply With Quote
  #48  
Old March 9th, 2013, 02:24 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
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){}

}

Reply With Quote
  #49  
Old March 9th, 2013, 02:37 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
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.

Reply With Quote
  #50  
Old March 10th, 2013, 08:38 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 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?

Reply With Quote
  #51  
Old March 10th, 2013, 10:02 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
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.

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