Welcome to Dream.In.Code
Become a Java Expert!

Join 149,443 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,251 people online right now. Registration is fast and FREE... Join Now!




Colors and JButtons

 
Reply to this topicStart new topic

Colors and JButtons

miranova
24 Jun, 2007 - 05:05 AM
Post #1

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 6


My Contributions
Hello , I am currently doing an assignment where I have to programm the Mastermind Game. My question is , I have 6 buttons from which the colors are chosen. My problem is when the user clicks on the button ,then starting with the first button [0][0],the color clicked should be assigned to the buttons - at least the first four for starters. This is a snippet of the code
CODE

public void actionPerformed(ActionEvent event) {
        for (int i = 0; i < color_buttons.length; i++) {
            color_buttons[i].addActionListener(this);
            if (event.getSource() == color_buttons[i]) {

                for (int k = 0; k <= 0; k++) {
                    // for(int j=0;j<=0;j++){
                    int j = 0;
                    if (j == 0)
                        guess_buttons[j][k].setBackground(color_selected[i]);
                    k++;
                    
                    repaint();

                }
            }
        }




Thanks
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Colors And JButtons
24 Jun, 2007 - 05:59 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
From the code you have shown all I can tell is that
the loop :
QUOTE
for (int k = 0; k <= 0; k++) {

doesn't seem right because
CODE
guess_buttons[j][k].setBackground(color_selected[i]);

is never reached. That is because of k<=0. So change that condition
to k<=4 or some other value you think that is appropriate.
That's it for now, if there are still problems post the rest of the code.

This post has been edited by PennyBoki: 24 Jun, 2007 - 06:00 AM
User is offlineProfile CardPM
+Quote Post

miranova
RE: Colors And JButtons
24 Jun, 2007 - 12:31 PM
Post #3

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 6


My Contributions
QUOTE(PennyBoki @ 24 Jun, 2007 - 06:59 AM) *

From the code you have shown all I can tell is that
the loop :
QUOTE
for (int k = 0; k <= 0; k++) {

doesn't seem right because
CODE
guess_buttons[j][k].setBackground(color_selected[i]);

is never reached. That is because of k<=0. So change that condition
to k<=4 or some other value you think that is appropriate.
That's it for now, if there are still problems post the rest of the code.



I have changed the code a bit- but I still haven't quite got it. When I click on the button I get the color I want on the the first button (I have an array of buttns [4][8], columns and rows). Now when I click on another color which should show on the second button ([1][0]) , I get another color but still on the first button. I think I should increase the counter for the second position of the column but dont know how.
CODE


    public void actionPerformed(ActionEvent event) {
        
        

        for (int i = 0; i < color_buttons.length; i++) {
            color_buttons[i].addActionListener(this);
            if (event.getSource() == color_buttons[i]) {

                for (int k = 0; k <1; k++) {
                    for (int j = 0; j <1; j++) {
                        guess_buttons[j][k].setBackground(color_selected[i]);


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Colors And JButtons
24 Jun, 2007 - 12:49 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
As I said I can't tell much unless I have more code.
But I think the problem is here:

CODE
for (int k = 0; k <1; k++) {
                    for (int j = 0; j <1; j++) {
                        guess_buttons[j][k].setBackground(color_selected[i]);


this code will only assign wanted color to the [0][0] button because of the condition that k<1

try this:
CODE
for (int k = 0; k <4; k++) {
                    for (int j = 0; j <8; j++) {
                        guess_buttons[j][k].setBackground(color_selected[i]);

User is offlineProfile CardPM
+Quote Post

miranova
RE: Colors And JButtons
24 Jun, 2007 - 12:59 PM
Post #5

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 6


My Contributions
Is there a way I can send u my entire code - without the whole world seeing it ? smile.gif . Tried -PM ing but didnt work
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Colors And JButtons
24 Jun, 2007 - 01:13 PM
Post #6

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
There is a way but the point is that the whole world could see so that if someone has similar problem she/he could find guidance here. This is a free community(one of the things I like most about this place) so everybody should share. That is where I stand sorry.
User is offlineProfile CardPM
+Quote Post

miranova
RE: Colors And JButtons
24 Jun, 2007 - 01:21 PM
Post #7

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 6


My Contributions
[Sorry my bad ,didn't mean to sound selfish Here is my code then
CODE

package gui;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MasterMindGui extends JFrame implements ActionListener {

    Container contPane = getContentPane();

    Graphics g;

    private JButton color_buttons[] = new JButton[6];

    private Color color_selected[] = new Color[6];

    private int unknown_color[] = new int[4];

    private JButton guess_buttons[][];

    private JButton result_box[][];
    
    private JButton start_button,reset_button;

    private JPanel button_panel,guess_panel,text_panel;

//    private JLabel text_label;

    private int rows;

    private int columns;

    private final static int number_of_trials = 8; // number of trials

    // inititalise with 1

    public MasterMindGui() {

        super();
        setTitle("MasterMind");

        rows = 8;
        columns = 4;
        button_panel = new JPanel(new GridLayout(8, 1));
        guess_panel = new JPanel(new GridLayout(rows, columns, 10, 10));
        text_panel = new JPanel(new GridLayout(rows, columns, 10, 10));

        guess_buttons = new JButton[4][8];
        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                guess_buttons[i][j] = new JButton();
                guess_buttons[i][j].setEnabled(false);
                guess_panel.add(guess_buttons[i][j]);

            }
        }
        result_box = new JButton[4][8];
        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                result_box[i][j] = new JButton("  ");
                result_box[i][j].setEnabled(false);
                result_box[i][j].setBackground(Color.WHITE);
                text_panel.add(result_box[i][j]);
            }
        }

        for (int i = 0; i < 4; i++) {
            int comp_color = (int) (Math.random() * 6);
            unknown_color[i] = comp_color;
        }

        color_buttons[0] = new JButton();
        color_selected[0] = new Color(240, 0, 0);
        button_panel.add(color_buttons[0]);
        color_buttons[0].setBackground(color_selected[0]);

        color_buttons[1] = new JButton();
        color_selected[1] = new Color(255, 255, 0);
        button_panel.add(color_buttons[1]);
        color_buttons[1].setBackground(color_selected[1]);

        color_buttons[2] = new JButton();
        color_selected[2] = new Color(141, 182, 205);
        button_panel.add(color_buttons[2]);
        color_buttons[2].setBackground(color_selected[2]);

        color_buttons[3] = new JButton();
        color_selected[3] = new Color(0, 0, 0);
        button_panel.add(color_buttons[3]);
        color_buttons[3].setBackground(color_selected[3]);

        color_buttons[4] = new JButton();
        color_selected[4] = new Color(255, 165, 0);
        button_panel.add(color_buttons[4]);
        color_buttons[4].setBackground(color_selected[4]);

        color_buttons[5] = new JButton();
        color_selected[5] = new Color(124, 252, 0);
        button_panel.add(color_buttons[5]);
        color_buttons[5].setBackground(color_selected[5]);

        for (int i = 0; i < color_buttons.length; i++) {
            color_buttons[i].addActionListener(this);

        }
        start_button = new JButton("Start");
        reset_button= new JButton("Reset");
        
        button_panel.add(start_button);
        button_panel.add(reset_button);
        contPane.add(button_panel, BorderLayout.WEST);
        contPane.add(guess_panel, BorderLayout.CENTER);
        contPane.add(text_panel, BorderLayout.EAST);
    

    }

    public void actionPerformed(ActionEvent event) {
        for (int i = 0; i < color_buttons.length; i++) {
            color_buttons[i].addActionListener(this);
            if (event.getSource() == color_buttons[i]) {

                for (int k = 0; k < 8; k++) {
                    for (int j = 0; j < 4; j++) {
                        guess_buttons[j][k].setBackground(color_selected[i]);

                    }

                
                }
            }

        }

    

    }

}


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Colors And JButtons
24 Jun, 2007 - 02:40 PM
Post #8

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
this way:
QUOTE
for (int k = 0; k < 8; k++) {
for (int j = 0; j < 4; j++) {
guess_buttons[j][k].setBackground(color_selected[i]);

}

all of the buttons are painted with one color.
Try it without the for loops , declare k and j as a global variables and set them to 0;
then after every event increase j++ up to 3
then increase k++ and set j=0 and again
....
...
...
and just stop when k reaches 8

This post has been edited by PennyBoki: 24 Jun, 2007 - 02:40 PM
User is offlineProfile CardPM
+Quote Post

miranova
RE: Colors And JButtons
27 Jun, 2007 - 08:55 AM
Post #9

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 6


My Contributions
hi Penny , thanks a lot, it does work , I tried that out but for some reason I never quite get beyond the second row. I'm getting an arrayindexoutofboundsexception, so obviously there is something wrong with my array and I think it's my color_selected[i] array. Still haven't figured out what it is though.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Colors And JButtons
27 Jun, 2007 - 12:41 PM
Post #10

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Maybe the problem is in here

QUOTE
for (int i = 0; i < color_buttons.length; i++) {


instead of color_buttons.length
try color_selected.length

I said maybe, post the new code to be more sure
User is offlineProfile CardPM
+Quote Post

miranova
RE: Colors And JButtons
28 Jun, 2007 - 10:48 AM
Post #11

New D.I.C Head
*

Joined: 5 Jun, 2007
Posts: 6


My Contributions
int k=0;//
int j=0; // both declared globally

guess_buttons[j][k].setBackground(color_selected[i]);
k++;

when I do this then my first two rows are shown after which i get the exception crazy.gif
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Colors And JButtons
29 Jun, 2007 - 05:57 AM
Post #12

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
now i"ll make this very simple:
post the new code along with the main function
and every thing in it, I do try to help here
and when I say the new code I mean all of it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 12:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month